首页 > 解决方案 > 'Null' 不是 '(String, dynamic) => void' of 'f' 类型的子类型)

问题描述

在这里,我们从模型中获取 id 和 msg。我收到这个错误_TypeError (type '(dynamic) => Null' is not a subtype of type '(String, dynamic) => void' of 'f') at theforEach((achat)```。

以下是 home.dart 中的问题部分:

Future<List<Chat>> _getAllChat() async {

    var result = await _chatService.getAllChat();
    // List<Chat> _List = List<Chat>();
    List<Chat> _List = [];

    if (result != null) {
      var Chats = json.decode(result.body);
      Chats.forEach((achat) {
        var model = Chat();
        model.id = achat['id'];
        model.msg = achat['msg'];
        setState(() {
          _List.add(model);
        });
      });
    }
    return _List;
  }

这是 Chat.dart 模型:

class Chat {
  late int id;
  late int userId;
  late int user2Id;
  late String msg;
}

标签: flutterdart

解决方案


推荐阅读