我尝试使用 moviedb API 从 Internet 获取数据,我按照 https://flutter.io/cookbook/networking/fetch-data/ 上的教程进行操作
但我收到以下错误。
Invalid argument(s): Illegal argument in isolate message : (object is a closure - Function 'createDataList':.)
这是我的代码
Future<List<DataModel>> fetchData() async{
final response = await http.get("https://api.themoviedb.org/3/movie/now_playing?api_key=d81172160acd9daaf6e477f2b306e423&language=en-US");
if(response.statusCode == 200){
return compute(createDataList,response.body.toString());
}
}
List<DataModel> createDataList(String responFroJson) {
final parse = json.decode(responFroJson).cast<Map<String, dynamic>>();
return parse.map<DataModel> ((json) => DataModel.fromtJson(json)).toList();
}
错误消息的屏幕截图
最佳答案
compute
只能采用顶级函数,不能采用实例或静态方法。
顶级函数是不在类中声明的函数 而不是在另一个函数中
List<DataModel> createDataList(String responFroJson) {
...
}
class SomeClass { ... }
应该修复它。
https://docs.flutter.io/flutter/foundation/compute.html
R is the type of the value returned. The callback argument must be a top-level function, not a closure or an instance or static method of a class.
关于flutter - 无效参数 : Illegal argument in isolate message : (object is a closure - Function 'createDataList' :. ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51998995/