【问题标题】:Fetching document snapshots from firestore and mapping to them to a dart object从 firestore 获取文档快照并将其映射到 dart 对象
【发布时间】:2020-08-12 10:24:13
【问题描述】:

一直在尝试使用 List 类型的流构建器从 Firestore 中的嵌套集合中获取文档列表。在从数据库服务调用文档时,长度被调用为 null.Kindly Assist。

   //mapping firestore data to a dart object
     Trip.fromFirestore(Map<String, dynamic> doc)
  : title = doc['title'],
    tripId = doc['tripId'],
    budget = doc['budget'],
    tripType = doc['tripType'],
    location = doc['location'],
    timestamp = doc['timestamp'],
    tripDescription = doc['tripDescription'],
    tripDates = doc['tripDates'];

 //Database code:
Stream<List<Trip>> getUserTrips(String userID) {
return _db
    .collection('trips')
    .document(userID)
    .collection('usertrips')
    .snapshots()`enter code here`
    .map((query) => query.documents)
    .map((snapshot) => snapshot
    .map((document) => Trip.fromFirestore(document.data))
    .toList());

}

//Homepage:
SliverToBoxAdapter(
        child: StreamBuilder<List<Trip>>(
          stream: Database().getUserTrips(user.userId),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Loader();
            }

            final trips = snapshot.data;

            return ListView.builder(
              shrinkWrap: true,
              itemCount: trips.length,
              itemBuilder: (context, index) {
                //print(snapshot.data[index].title);
                return Text(trips[index].title);
                //TripCard();
              },
            );
          },
        ),
      ),

//The Error:
 The getter 'length' was called on null.
 Receiver: null
 Tried calling: length
 The relevant error-causing widget was
 StreamBuilder<List<Trip>> 

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    这不应该是最终的,您正在使用流,并且流会发生变化。

      final trips = snapshot.data;
    

    据我所知,快照的类型是列表。流应如下所示。

    SliverToBoxAdapter(
            child: StreamBuilder(
              stream: _db
                        .collection('trips')
                        .document(userID)
                        .collection('usertrips')
                        .snapshots(),
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return Loader();
                }
    
                var trips = snapshot.data.documents; //edit
    
                return ListView.builder(
                  shrinkWrap: true,
                  itemCount: trips.length,
                  itemBuilder: (context, index) {
                    //print(snapshot.data[index].title);
                    return Text(trips[index].title);
                    //TripCard();
                  },
                );
              },
            ),
          ),
    

    【讨论】:

    • 感谢您的回答,如果我想使用上面发布的数据库服务,我可以将 firestore 文档映射到 dart 对象,我该怎么做?
    • 你可以这样做,信息将保存在trips变量中,因为它包含集合中的所有文档。
    • 是的,我使用了将 Firestore 文档映射到对象的方法,但似乎有一个我无法找到的错误,它与文档的长度有关。请协助定位错误.
    • 更改后是否出现同样的错误?另外,我编辑了我的答案
    • 感谢您的建议,将不得不调查。谢谢。
    猜你喜欢
    • 2019-12-12
    • 2023-01-13
    • 2021-09-03
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 2020-06-20
    • 2020-12-16
    • 2023-03-19
    相关资源
    最近更新 更多