在Flutter Web中比较两个字符串/比较两个Cloud Firestore对象的documentID

科济米尔

尝试比较DocumentID和List中Firestore DocumentReference对象的ID。并遇到了这种奇怪的行为。我想从列表中获取正确的对象。我究竟做错了什么?

DocumentReference _resolveReferenceFromID(
      List<DocumentReference> references, String documentID) {
    references.forEach((element) {
      print('element id: ${element.documentID}');
      print('document id: $documentID');
      if (element.documentID == documentID) {
        return element;
      }
    });
    print('something is wrong here');
    return null;
  }

这是一个输出:

element id: W24buSwq7cYOahJz6Gdq
document id: W24buSwq7cYOahJz6Gdq
element id: cBOIXvebYYIpox5cAcct
document id: W24buSwq7cYOahJz6Gdq
something is wrong here

扑--version:

Flutter 1.22.0-2.0.pre.36 • channel master • https://github.com/flutter/flutter.git
Framework • revision d30e36ba8c (6 days ago) • 2020-08-21 22:36:03 -0400
Engine • revision d495da20d0
Tools • Dart 2.10.0 (build 2.10.0-49.0.dev)

扑医生:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel master, 1.22.0-2.0.pre.36, on Microsoft Windows [Version 10.0.18362.1016], locale en-US)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
[√] Chrome - develop for the web
[√] Android Studio (version 4.0)
[√] VS Code (version 1.48.2)
[√] Connected device (3 available)

• No issues found!
rn

您正在forEach用来遍历元素。return element里面forEach只能从函数,而不是从周围的函数参数返回_resolveReferenceFromID功能。

更改为:

DocumentReference _resolveReferenceFromID(
      List<DocumentReference> references, String documentID) {
    for (var element in references) {
      print('element id: ${element.documentID}');
      print('document id: $documentID');
      if (element.documentID == documentID) {
        return element;
      }
    }
    print('something is wrong here');
    return null;
  }

然后,return element;将从所需的函数返回。

另请参阅:https : //dart.dev/guides/language/effective-dart/usage#avoid-using-iterableforeach-with-a-function-literal

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Flutter 比较一个值和两个字符串

比较两个字符串对象以获取匹配的字符串

通过两个字符串对象创建日期/时间对象

将两个字符串数组合并为一个对象数组

在mongo聚合中匹配两个字符串对象字段

如何从两个字符串的对象列表绑定AutoCompleteTextView?

Mapstruct:将对象列表映射为两个字符串/ UUID列表

比较Java中的两个对象..每个对象都有两个字段

按某些两个字段比较不同对象的两个列表

Java。如何从两个字符串数组或列表中删除相同的字符串对象

将具有两个字符串参数的对象转换为逗号分隔的字符串

在Python中比较两个字符(在字符串中)的工作方式

如何在Java中比较两个字符串日期?

如何在python中比较这两个字符串?

在 C 中比较两个字符串时遇到问题

如何在NUnitLite中比较两个字符串?

如果条件如何在javascript中比较两个字符串

如何在javascript中比较两个字符串日期?

如何在Makefile中比较两个字符串变量

如何在Perl中比较两个字符串?

如何在Bash的'if'语句中比较两个字符串变量?

如何在ASP.NET VB中比较两个字符串

AWK:如何在一行中比较两个字符串

当您在python中比较两个字符串时会发生什么

在Haskell中比较两个字符串

如何在Tensorflow中比较两个字符串?

如何在 Access 中比较两个字符串?

使用JavaScript中的if条件在for循环中比较两个字符串

如何在bash中的if条件中比较两个字符串