Firebase 如何订购带有2个字段的cloud firestore快照?

Firebase 如何订购带有2个字段的cloud firestore快照?,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我正在尝试订购一个包含2个字段的cloud firestore快照。我在用飞镖 我有一个错误: NoSuchMethodError:对null调用了getter“lenght”。接收者:空。尝试呼叫:长度。另请参见:https//flatter.dev/docs/testing/errors 我正在使用cloud\u firestore:^2.2.0 我的代码: Stream<List<Friend>> getFriends() { return _db

我正在尝试订购一个包含2个字段的cloud firestore快照。我在用飞镖 我有一个错误: NoSuchMethodError:对null调用了getter“lenght”。接收者:空。尝试呼叫:长度。另请参见:https//flatter.dev/docs/testing/errors

我正在使用cloud\u firestore:^2.2.0

我的代码:

  Stream<List<Friend>> getFriends() {
    return _db
        .collection('profile')
        .doc(userId)
        .collection('friends')
        .orderBy('status', descending: true)
        .orderBy('displayName', descending: false)
        .snapshots()
        .map((snapshot) => snapshot.docs
        .map((doc) => Friend.fromJason(doc.data()))
        .toList());
  }
Stream getFriends(){
返回_db
.collection('配置文件')
.doc(用户ID)
.collection('朋友')
.orderBy('status',降序:true)
.orderBy('displayName',降序:false)
.快照()
.map((快照)=>snapshot.docs
.map((doc)=>Friend.fromJason(doc.data())
.toList());
}
谢谢你的帮助

您所做的查询类型(在两个字段上排序)在Firestore上受支持,但确实需要不自动创建的索引

如果检查应用程序的调试输出,则在运行此查询时应该会发现另一条错误消息。在此错误消息中,有一个链接将您带到Firebase控制台,其中包含已填写的所有信息。在那里,只需单击一下即可创建索引,并使查询正常工作。

只需添加索引查询。下面是一个例子:请检查。构建索引查询需要2-5分钟。然后你就可以看到这样的画面了


注意:如果您使用VS代码,请转到调试/终端。您将通过单击此链接找到直接链接。索引查询将为您自动生成。链接会将您重定向到Firestore索引控制台

Hey Seb Pro。你有什么收获吗?两个人试图用下面的答案帮助你。如果我的答案有用,请单击向上投票按钮(▲) 在它的左边(您可能还没有足够的声誉)。如果它回答了您的问题,请单击复选标记(✓) 接受它。这样别人就会知道你已经得到了(足够的)帮助。也请参见