收到此错误-输入"Future< dynamic>"不是类型"List< dynamic>"的子类型

收到此错误-输入

问题描述:

每当尝试调用将来的数据并尝试转换为 List 时,都会返回错误

Whenever trying to call future data and trying converting to List, it returns the error

"Future"类型不是"List"类型的子类型

type 'Future' is not a subtype of type 'List'

尝试类型转换,但无济于事

Tried type-casting, but no help

在HomePage.dart上

On HomePage.dart

final getPost = NetworkFile().getPosts();
  List posts;

  void getPostsList() {
    setState(() {
      var res = getPost;
      posts = res as List<dynamic>;
      print(posts);
    });
  } 

在Network.dart上

On Network.dart

class NetworkFile{

Future<dynamic> getPosts() async {
    var response = await http.get('$kBlogURL' + 'posts?_embed');
    Iterable resBody = await jsonDecode(response.body.toString());
    return resBody;
  }
} 

您正在解码响应及其类型为 dynamic List .处理它的方法很少.您可以创建一个简单的PODO类并对其进行强制转换/映射.或只是如下所示:

You are decoding the response and its a List of type dynamic. There are few method to handle it. You can create a simple PODO class and cast/mapped to it. Or just do like below:

List posts = [];

void getPostsList() async {
  final fetchedPosts = await NetworkFile().getPosts();
  setState(() {
    posts = fetchedPosts;
  });
  print(posts);
}

这是一篇有关PODO的不错的文章