简体   繁体   English

输入&#39;未来<List<Data> &gt;&#39; 不是 &#39;List 类型的子类型<Data> &#39; 在类型转换中

[英]type 'Future<List<Data>>' is not a subtype of type 'List<Data>' in type cast

I am getting the data from server and trying to set it in a grid view but I am getting error:我正在从服务器获取数据并尝试将其设置在网格视图中,但出现错误:

type 'Future<List<Data>>' is not a subtype of type 'List<Data>' in type cast

Here is my Data Class:这是我的数据类:

class Data with ChangeNotifier {
Data({
  this.name,
  this.image,
});

final String image;
final String name;


factory Data.fromJson(Map<String, dynamic> json) {
print(json['title'].toString());
return Data(
  name: json['title'].toString(),
  image: json['image'].toString(),
 );

}
}

And this is my Data_screen where I am calling this:这是我的 Data_screen,我在那里调用它:

 var datas= Provider.of<Datas>(context).fetchData() as List<Data>;
 var datalength = datas.length;

Widget:小部件:

 Expanded(
            child: GridView.builder(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(

                crossAxisSpacing: 10,
                mainAxisSpacing: 10,
              ),
              padding: EdgeInsets.only(left: 28, right: 28, bottom: 58),
              itemCount: datas.length,

              itemBuilder: (context, index) => DataCard(
                datas[index],
                index: index,
                onPress: () {
                },
              ),
            ),
          ),

And in Datas.dart:在 Datas.dart 中:

 Future<List<Data>> fetchData() async {
 var response = await http.get(url);
 var responseJson = json.decode(response.body);
 print(responseJson);
 return (responseJson['datas'])
    .map<Data>((p) => Data.fromJson(p))
  .toList();

} }

The message is so clear.信息如此清晰。 You cannot cast from List<Data> to Future<List<Data>> .您不能从List<Data>Future<List<Data>> Try to use:尝试使用:

List<Data> fetchData() async {
 var response = await http.get(url);
 var responseJson = json.decode(response.body);
 print(responseJson);
 return (responseJson['datas'])
    .map<Data>((p) => Data.fromJson(p))
  .toList();
}

OR return a new Future with the List<Data>或者用List<Data>返回一个新的Future

Future<List<Data>> fetchData() async {
 var response = await http.get(url);
 var responseJson = json.decode(response.body);
 print(responseJson);
 var response = (responseJson['datas'])
    .map<Data>((p) => Data.fromJson(p))
  .toList();
 return Future.value(response)
}

It just an idea(without testing)这只是一个想法(未经测试)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 输入'未来<dynamic> ' 不是类型 'List 的子类型<application> ?</application></dynamic> - type 'Future<dynamic>' is not a subtype of type 'List<Application>?' 未来<list<note> &gt; 不是 List 类型的子类型<note></note></list<note> - Future<List<Note>> is not a subtype of type List<Note> 输入'列表<dynamic> ' 不是类型 'List 的子类型<expense> ? 在类型转换中</expense></dynamic> - type 'List<dynamic>' is not a subtype of type 'List<Expense>?' in type cast 输入'列表<object?> ' 不是类型转换中类型 'List&lt;&gt;' 的子类型</object?> - type 'List<Object?>' is not a subtype of type 'List<>' in type cast “Null”类型不是“List”类型的子类型<map<String,Object> &gt;&#39; 类型转换 - type 'Null' is not a subtype of type 'List<map<String,Object>>' in type cast 颤振:输入“未来”<dynamic> &#39; 不是类型 &#39;Uint8List&#39; 的子类型 - Flutter: type 'Future<dynamic>' is not a subtype of type 'Uint8List' Flutter:输入“未来”<bool?> ' 不是 'FutureOr 类型的子类型<bool> ' 在类型转换中</bool></bool?> - Flutter: type 'Future<bool?>' is not a subtype of type 'FutureOr<bool>' in type cast I/flutter(2680):输入“列表”<dynamic> &#39; 不是类型 &#39;Map 的子类型<dynamic, dynamic> &#39; 在类型转换中 - I/flutter ( 2680): type 'List<dynamic>' is not a subtype of type 'Map<dynamic, dynamic>' in type cast _TypeError(输入&#39;列表<dynamic> &#39; 不是类型 &#39;PlaceDetails&#39; 的子类型) - _TypeError (type 'List<dynamic>' is not a subtype of type 'PlaceDetails') “Null”类型不是“List”类型的子类型<Widget> &#39; - type 'Null' is not a subtype of type 'List<Widget>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM