You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dart报错:GroupBy类未定义fromMap方法,求解决方案

解决The method 'fromMap' isn't defined for the type 'GroupBy'错误

问题根源

你的GroupBy类仅定义了fromJson工厂方法,并未实现fromMap方法,但在loadPreviousEvents函数中调用了GroupBy.fromMap(e),导致方法未找到错误。此外,res.body是字符串类型,不能直接强转为List,需通过json.decode解析。


解决方案一:直接使用现有fromJson方法

修改loadPreviousEvents方法,替换fromMap为已有的fromJson,同时修正响应解析逻辑:

import 'dart:convert';
import 'package:http/http.dart' as http;

loadPreviousEvents() async {
    var url = 'http://xxxxxxx/getEvents.php';
    var res = await http.get(Uri.parse(url));
    // 将响应字符串解析为List<Map<String, dynamic>>
    var response = json.decode(res.body) as List;
    print(response);
    // 用fromJson替代fromMap,无需async/await(fromJson是同步方法)
    List<GroupBy> events = response.map((e) => GroupBy.fromJson(e)).toList();
    // 可在此添加对events的后续处理逻辑
}

解决方案二:添加fromMap工厂方法到GroupBy类

若习惯使用fromMap命名,可在GroupBy类中添加与fromJson逻辑一致的方法:

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());

  // 让fromJson调用fromMap,减少代码重复
  factory GroupBy.fromJson(Map<String, dynamic> json) => GroupBy.fromMap(json);

  // 新增fromMap方法
  factory GroupBy.fromMap(Map<String, dynamic> map) => GroupBy(
        id: map["id"] == null ? null : map["id"],
        date: map["date"] == null ? null : DateTime.parse(map["date"]),
        selectdate: map["selectdate"] == null
            ? null
            : DateTime.parse(map["selectdate"]),
        descript: map["descript"] == null ? null : map["descript"],
        title: map["title"] == null ? null : map["title"],
        idEventDate:
            map["id_event_date"] == null ? null : map["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的解析部分(同样需修正响应解析):

loadPreviousEvents() async {
    var url = 'http://xxxxxxx/getEvents.php';
    var res = await http.get(Uri.parse(url));
    var response = json.decode(res.body) as List;
    print(response);
    List<GroupBy> events = response.map((e) => GroupBy.fromMap(e)).toList();
    // 后续处理逻辑
}

内容的提问来源于stack exchange,提问作者Zana Souleymane Coulibaly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 23:35:23