无法从Firebase检索文档

时间:2020-05-20 04:35:38

标签: firebase flutter dart google-cloud-firestore

因此,我试图通过loadmap()方法从Firestore中的某个集合“ avtvity”中检索所有文档。我将函数放在initState()中,因为我想立即将数据添加到Place中,这是我创建的用于添加位置的类。但是,数据没有被放置在我的“位置”中,该位置是一个列表,这使我怀疑未调用loadMap()。

有什么解决办法吗?

代码如下:

class PlaceTrackerApp extends StatefulWidget {
   final FirebaseUser firebaseUser;

  PlaceTrackerApp({this.firebaseUser});
  @override
  _PlaceTrackerAppState createState() => _PlaceTrackerAppState();
}

class _PlaceTrackerAppState extends State<PlaceTrackerApp> {
  AppState appState = AppState();

  @override
  void initState() {
    super.initState();
    print(widget.firebaseUser);
    loadMap();    
  }

  static StreamBuilder<QuerySnapshot> loadMap() {
    return StreamBuilder(
        stream: Firestore.instance.collection('avtivity').snapshots(),
        builder: (context, snapshot) {
          if(!snapshot.hasData) return Text("Loading... Please wait,");
          for (int i = 0; i < snapshot.data.documents.length; i++) {
            StubData.places.add(new Place(
              id: snapshot.data.documents[i]['id'],
              latLng: snapshot.data.documents[i]['latlng'],
              name: snapshot.data.documents[i]['title'],
              description: snapshot.data.documents[i]['description'],
              starRating: snapshot.data.documents[i]['rating'],
              category: PlaceCategory.activities,
            ));
          }}
          );

}

这是数据库的屏幕截图: enter image description here

0 个答案:

没有答案