StreamBuilder arrayContains 和 RangeError(索引):无效值:有效值范围为空:0
StreamBuilder arrayContains and RangeError (index): Invalid value: Valid value range is empty: 0
作为我的新爱好,慢慢掌握 Flutter 和 Dart。我们将不胜感激您提供的任何建议或帮助!我正在尝试实现一个简单的好友系统,您可以在其中搜索另一个用户并将其添加为好友。
预期结果是:
- 登录用户转到朋友的个人资料并单击 'add friend' 按钮。
- Firestore 有一个用户集合。在该集合中是每个用户的文档,其中包含配置文件信息,包括一个数组 'friends'。单击 'add friend' 按钮会将登录用户的 UID 添加到第二个人的 'friends' 数组中。
- 然后在“好友”屏幕上,我使用 StreamBuilder 和 Listview.builder 将所有用户显示为卡片,这些卡片在 'friends' 数组中包含登录用户的 UID。
为了实现这一点,我想在 StreamBuilder 的流中使用 .where('friends', arrayContains: 'user.uid'),只显示那些用户屏幕列表中的卡片。
这是我写的代码:
class friendsList extends StatelessWidget {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return
StreamBuilder(
stream: Firestore.instance
.collection('users')
.where('friends', arrayContains: user.uid).snapshots(),
builder: (context, documents) {
if (documents.hasData) {
return
Container(
height: 180,
child:
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 1,
itemBuilder: (context, index) {
DocumentSnapshot friends = documents.data.documents[index];
但是,当我转到 运行 我的应用程序时,它显示错误:
`The following RangeError was thrown building:
RangeError (index): Invalid value: Valid value range is empty: 0
When the exception was thrown, this was the stack
List.[](dart:core-patch/growable_array.dart:149:60)
friendsList.build.<anonymous closure>.<anonymous closure>
备注:
- itemCount 现在被硬编码为 1。我只希望在测试时返回 1 个用户。一旦一切正常,我将用更多用户填充 Firestore。
- 当我将 arrayContains 换成更简单的东西时,例如 .where('displayName', isEqualTo: 'My Name').snapshots(), ,该应用程序在没有错误
- user.uid 已经过测试,确实包含预期的字符串
- 数组与值一起存在于 Firestore 中(请参阅 post 底部的 link)
- 我已经为我在 'users'
中使用的值设置了一个索引
有谁知道我能做些什么来解决这个错误并检索包含 friends 数组和登录用户 UID 的文档?
再次感谢任何建议和帮助。
Screenshot of Cloud Firestore is here
我离开了我的项目两天,并在最后一个小时重建了应用程序。令我惊讶的是,'.where('friends', arrayContains: user.uid)' 现在有效了!
当应用程序启动时,我现在有一个包含当前登录用户的好友的所有用户的 ListView。
完整系统的工作原理:
在每个用户的个人资料页面上都有一个浮动操作按钮,按下该按钮后,会将当前登录用户的 UID 添加到该朋友文档中的 'friends' 数组中。
使用 StreamBuilder 和 ListView,当登录用户随后转到他们的朋友页面时,他们会看到每个在 'friends' 数组中具有登录用户 UID 的用户
可能是您正在打印来自 firebase 的空数据的结果。
Stream<List<Model>> fetchStream({@required String userUID}) {
return nestsCollection
.where("participants", arrayContains: userUID)
.snapshots()
.map((snapshot) {
// print(snapshot.docs[0].data()['nestName']);
return snapshot.docs
.map((document) => Model.fromFirestore(document))
.toList();
});
}
作为我的新爱好,慢慢掌握 Flutter 和 Dart。我们将不胜感激您提供的任何建议或帮助!我正在尝试实现一个简单的好友系统,您可以在其中搜索另一个用户并将其添加为好友。
预期结果是:
- 登录用户转到朋友的个人资料并单击 'add friend' 按钮。
- Firestore 有一个用户集合。在该集合中是每个用户的文档,其中包含配置文件信息,包括一个数组 'friends'。单击 'add friend' 按钮会将登录用户的 UID 添加到第二个人的 'friends' 数组中。
- 然后在“好友”屏幕上,我使用 StreamBuilder 和 Listview.builder 将所有用户显示为卡片,这些卡片在 'friends' 数组中包含登录用户的 UID。
为了实现这一点,我想在 StreamBuilder 的流中使用 .where('friends', arrayContains: 'user.uid'),只显示那些用户屏幕列表中的卡片。
这是我写的代码:
class friendsList extends StatelessWidget {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return
StreamBuilder(
stream: Firestore.instance
.collection('users')
.where('friends', arrayContains: user.uid).snapshots(),
builder: (context, documents) {
if (documents.hasData) {
return
Container(
height: 180,
child:
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 1,
itemBuilder: (context, index) {
DocumentSnapshot friends = documents.data.documents[index];
但是,当我转到 运行 我的应用程序时,它显示错误:
`The following RangeError was thrown building:
RangeError (index): Invalid value: Valid value range is empty: 0
When the exception was thrown, this was the stack
List.[](dart:core-patch/growable_array.dart:149:60)
friendsList.build.<anonymous closure>.<anonymous closure>
备注:
- itemCount 现在被硬编码为 1。我只希望在测试时返回 1 个用户。一旦一切正常,我将用更多用户填充 Firestore。
- 当我将 arrayContains 换成更简单的东西时,例如 .where('displayName', isEqualTo: 'My Name').snapshots(), ,该应用程序在没有错误
- user.uid 已经过测试,确实包含预期的字符串
- 数组与值一起存在于 Firestore 中(请参阅 post 底部的 link)
- 我已经为我在 'users' 中使用的值设置了一个索引
有谁知道我能做些什么来解决这个错误并检索包含 friends 数组和登录用户 UID 的文档?
再次感谢任何建议和帮助。 Screenshot of Cloud Firestore is here
我离开了我的项目两天,并在最后一个小时重建了应用程序。令我惊讶的是,'.where('friends', arrayContains: user.uid)' 现在有效了!
当应用程序启动时,我现在有一个包含当前登录用户的好友的所有用户的 ListView。
完整系统的工作原理:
在每个用户的个人资料页面上都有一个浮动操作按钮,按下该按钮后,会将当前登录用户的 UID 添加到该朋友文档中的 'friends' 数组中。
使用 StreamBuilder 和 ListView,当登录用户随后转到他们的朋友页面时,他们会看到每个在 'friends' 数组中具有登录用户 UID 的用户
可能是您正在打印来自 firebase 的空数据的结果。
Stream<List<Model>> fetchStream({@required String userUID}) {
return nestsCollection
.where("participants", arrayContains: userUID)
.snapshots()
.map((snapshot) {
// print(snapshot.docs[0].data()['nestName']);
return snapshot.docs
.map((document) => Model.fromFirestore(document))
.toList();
});
}