【问题标题】:The method 'fromMap' isn't defined for the type 'GroupBy'没有为类型 \'GroupBy\' 定义方法 \'fromMap\'
【发布时间】:2022-10-30 23:02:48
【问题描述】:

我正在努力寻找解决问题的方法。 出现的错误是

没有为“GroupBy”类型定义方法“fromMap”。

我的模型

import 'dart:convert';

class GroupBy {
  GroupBy({
    this.id,
    this.date,
    this.selectdate,
    this.descript,
    this.title,
    this.idEventDate,
  });

  final int? id;
  final DateTime? date;
  final DateTime? selectdate;
  final String? descript;
  final String? title;
  final int? idEventDate;

  factory GroupBy.fromRawJson(String str) => GroupBy.fromJson(json.decode(str));

  String toRawJson() => json.encode(toJson());

  factory GroupBy.fromJson(Map<String, dynamic> json) => GroupBy(
        id: json["id"] == null ? null : json["id"],
        date: json["date"] == null ? null : DateTime.parse(json["date"]),
        selectdate: json["selectdate"] == null
            ? null
            : DateTime.parse(json["selectdate"]),
        descript: json["descript"] == null ? null : json["descript"],
        title: json["title"] == null ? null : json["title"],
        idEventDate:
            json["id_event_date"] == null ? null : json["id_event_date"],
      );

  Map<String, dynamic> toJson() => {
        "id": id == null ? null : id,
        "date": date == null
            ? null
            : "${date!.year.toString().padLeft(4, '0')}-${date!.month.toString().padLeft(2, '0')}-${date!.day.toString().padLeft(2, '0')}",
        "selectdate": selectdate == null
            ? null
            : "${selectdate!.year.toString().padLeft(4, '0')}-${selectdate!.month.toString().padLeft(2, '0')}-${selectdate!.day.toString().padLeft(2, '0')}",
        "descript": descript == null ? null : descript,
        "title": title == null ? null : title,
        "id_event_date": idEventDate == null ? null : idEventDate,
      };
}

这是我一直试图运行的代码,但没有成功。你能帮助我吗 ?

loadPreviousEvents() async {
    var url = 'http://xxxxxxx/getEvents.php';
    var res = await http.get(Uri.parse(url));
    var response = res.body as List;
    print(response);
    (response.map((e) async => await GroupBy.fromMap(e))).toList();
  }

【问题讨论】:

  • 您需要将fromMap 替换为fromJson

标签: flutter


【解决方案1】:

response.body 还没有被解码,所以它只是一个String,所以你试图在一个字符串上调用一个List 方法,它是map

据我所知,您有一个方法可以解码JSON 字符串,即 fromJson,所以试试这个:

loadPreviousEvents() async {
var url = 'http://xxxxxxx/getEvents.php';
var res = await http.get(Uri.parse(url));
var response = res.body as List;
print(response);
(response.map((e) async => await GroupBy.fromJson(e))).toList();
}

【讨论】:

    猜你喜欢
    • 2021-09-02
    • 2022-11-21
    • 2021-10-19
    • 2020-12-14
    • 2021-07-19
    • 2021-09-01
    • 2021-09-17
    • 2021-06-11
    • 2021-06-28
    相关资源
    最近更新 更多