flutter - 'Null' 不是 '(String, dynamic) => void' of 'f' 类型的子类型)
问题描述
在这里,我们从模型中获取 id 和 msg。我收到这个错误_TypeError (type '(dynamic) => Null' is not a subtype of type '(String, dynamic) => void' of 'f') at the
forEach((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;
}
解决方案
推荐阅读
- javascript - Javascript 更改标记名称的样式
- html - 如何使用 css 定位跨度中的特定文本
- c# - 使用 TLS 会话重用将文件上传到 C# 中的隐式 FTPS 服务器
- r - 是否有 R 函数可以使用两列或多列对矩阵进行排序?
- c# - 无法在 NET Core Web api 中添加 AddIdentity 服务
- html - 如何使用 css 和 html 对齐文本和图像?左右位置?
- javascript - 每次访问页面时 React 重新渲染
- python - 如何实现超时功能?
- json - 来自本地 JSON 文件的 SwiftUI 搜索字段
- react-native - React native Invariant Violation:requireNativeComponent:在UIManager中找不到“RNCViewPager”