【问题标题】:Comparing elements Flutter/FirebaseFirestore比较元素 Flutter/FirebaseFirestore
【发布时间】:2021-07-04 03:34:11
【问题描述】:

我在获取用户时遇到问题,他们的电子邮件在其他用户的数组“SeniorList”中。当我有一个用户发送电子邮件时,它会打印我的空数组

_seniorList

我是 Firebase 新手,所以每条建议都很重要。

这是 Firestore 数据库结构:

https://imgur.com/yrtJ4RZ

https://imgur.com/z3gurUq

我试过的代码:

 Future<List<String>> getSeniorList() async {
  var _currentUser = FirebaseAuth.instance.currentUser;
  List<String> list;
DocumentSnapshot data = await FirebaseFirestore.instance
  .collection('users')
  .doc(_currentUser!.uid)
  .get();

list = List.from(data['SeniorList']);
return list;
}



Future<void> printSeniorNameList() async {
 final List<String> _seniorList = await getSeniorList();
 print(_seniorList);

 final QuerySnapshot result = await FirebaseFirestore.instance
  .collection('users')
  .where('email', arrayContainsAny: _seniorList)
  .get();

 final List<DocumentSnapshot> documents = result.docs;
 print(documents);
}

PS。如果你能告诉我如何以正确的方式粘贴图片,我将不胜感激!

【问题讨论】:

  • 列表为空,因为您的文档中没有任何名为 SeniorList 的属性
  • 您是否尝试过使用 whereIn 而不是 arrayContainsAny?从我的角度来看,它更有意义
  • whereIn 没有改变;
  • 图片的添加方式与链接相同,但开头带有感叹号:![Valid XHTML](https://w3.org/Icons/valid-xhtml10)。请参考Markdown help。您能否也更新您的答案,添加对解决您问题的确切更改的描述?这将有助于使答案更加直观。

标签: firebase flutter google-cloud-firestore


【解决方案1】:

这样解决了:

Future<List<String>> getSeniorList() async {
 var _currentUser = FirebaseAuth.instance.currentUser;
 List<String> list;
 DocumentSnapshot data = await FirebaseFirestore.instance
  .collection('users')
  .doc(_currentUser!.uid)
  .get();

 list = List.from(data['SeniorList']);
 return list;
}

Future<bool> isSeniorAlreadyInTheList(String checkemail) async {
 final List<String> _seniorList = await getSeniorList();
 if (_seniorList.contains(checkemail)) {
  return true;
 } else {
    print('Email not in a Senior List');
    return false;
   }
}

Future<void> printSeniorNameWhoseEmailInTheList(String checkemail) async {
 bool exists = await isSeniorAlreadyInTheList(checkemail);
 Map<String, dynamic>? seniorName;
 if (exists) {
  var result = await FirebaseFirestore.instance
    .collection('users')
    .where('email', isEqualTo: checkemail)
    .limit(1)
    .get();
  seniorName = result.docs[0].data();
  print(seniorName!['username']);
 } else
    print('That users email is not in a SeniorList!');
  }

已经为我工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    相关资源
    最近更新 更多