无效的参数:隔离消息中的参数非法:(对象是闭包-函数'createDataList':.) [英] Invalid argument(s): Illegal argument in isolate message : (object is a closure - Function 'createDataList':.)

查看:498
本文介绍了无效的参数:隔离消息中的参数非法:(对象是闭包-函数'createDataList':.)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用moviedb API从Internet上获取数据,我在 https:上遵循了该教程: //flutter.io/cookbook/networking/fetch-data/

I tried to fetch data from the internet with moviedb API, I followed the tutorial at https://flutter.io/cookbook/networking/fetch-data/

但出现以下错误.

无效的参数:隔离消息中的非法参数:(对象是一个闭包-函数'createDataList':.)

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

错误消息的屏幕截图

Screenshot of the error message

推荐答案

compute只能使用顶级功能,而不能使用实例或静态方法.

compute can only take a top-level function, but not instance or static methods.

顶级函数是声明为不在类内部的函数 而不是在另一个函数之内

Top-level functions are functions declared not inside a class and not inside another function

List<DataModel> createDataList(String responFroJson) {
...
}

class SomeClass { ... }

应该修复它.

https://docs.flutter.io/flutter/foundation/compute.html

R是返回值的类型.回调参数必须是顶级函数,而不是类的闭包,实例或静态方法.

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.

这篇关于无效的参数:隔离消息中的参数非法:(对象是闭包-函数'createDataList':.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆