本文介绍了无效的参数:隔离消息中的参数非法:(对象是闭包-函数'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/

但出现以下错误.

这是我的代码

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

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

11-01 17:50