类型"Future< dynamic>"不是类型"List< dynamic>"的子类型铸型 [英] type 'Future<dynamic>' is not a subtype of type 'List<dynamic>' in type cast

查看:67
本文介绍了类型"Future< dynamic>"不是类型"List< dynamic>"的子类型铸型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从我的Firestore中获取所有文档到确定的集合中,然后,我想将它们设置在文档列表中,该列表的每个位置都代表一个文档.但是,在编写代码时,我收到此错误:

I'm trying to get all the documents inside a determined collection from my firestore and after that, I want to set them in a list of documents which each position of the list represents a document. However, when I made the code, I received this error:

类型'Future'不是类型转换类型'列表'的子类型

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

import 'package:cloud_firestore/cloud_firestore.dart';

class Restaurant{

  String keyword;
  String state;
  String address;
  String city;
  int evaluation;
  String image;
  String phone;
  double rating;
  String schedule;
  String text;
  String title;

  Restaurant({
    this.keyword,
    this.state,
    this.address,
    this.city,
    this.evaluation,
    this.image,
    this.phone,
    this.rating,
    this.schedule,
    this.text,
    this.title
  });

  factory Restaurant.fromDatabase(DocumentSnapshot document){
    return Restaurant(
      keyword: document["keyword"] as String,
      state: document["state"] as String,
      address: document["address"] as String,
      city: document["city"] as String,
      evaluation: document["evaluation"],
      image: document["image"] as String,
      phone: document["phone"] as String,
      rating: document["rating"],
      schedule: document["schedule"] as String,
      text: document["text"] as String,
      title: document["title"] as String
    );
  }
}

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cruke_app/restaurant.dart';

class RestaurantsViewModel {
  static List<Restaurant> restaurants;
  static List<DocumentSnapshot> allDocuments;

  static loadDocuments() async{
    var documents = await Firestore.instance.collection('restaurant').getDocuments();
    return documents;
  } 

  static Future loadRestaurants() async {
    try {

      restaurants = new List<Restaurant>();
      allDocuments = new List<DocumentSnapshot>();

      var documents = loadDocuments() as List;
      documents.forEach((dynamic snapshot) {
        allDocuments.add(snapshot);
      });

      for(int i=0; i < allDocuments.length; i++){
        restaurants.add(new Restaurant.fromDatabase(allDocuments[i]));
      }
    } catch (e) {
      print(e);
    }
  }
}

推荐答案

您需要在 Future 调用之前添加 await 关键字:

you need to add await keyword before the Future call:

var documents = await loadDocuments() as List;

此外,您还需要像这样更改 loadDocuments()方法的返回值:

also, you need to change the return of the loadDocuments() method like this:

  static loadDocuments() async{
    var documents = await   Firestore.instance.collection('restaurant').getDocuments();
    return documents.documents; //this returns a List<DocumentSnapshot>
  } 

这篇关于类型"Future&lt; dynamic&gt;"不是类型"List&lt; dynamic&gt;"的子类型铸型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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