可以将无效的表达式可以用作for-In循环中的迭代器

发布于 2025-01-24 09:48:31 字数 1818 浏览 3 评论 0原文

  • 我在使用Flutter& Firebase(Firestore),我进行了研究,无法找到任何有用的东西。

  • 我想将我从firestore获得的流变成小部件流streambuilder()

  • 这是firestore对象初始化最终FirebaseFirestore _firestore = firebasefirestore.instance;

  • 这是我尝试从Firestore获取数据的方法

      void messagesstream()async {
      等待(_firestore.collection('message')中的(var snapshot)。快照()){
        对于(snapshot.docs中的var消息){
          打印(message.data());
        }
      }
    }
     
  • 这是streambuilder widget insdie my widget tree =>

StreamBuilder<QuerySnapshot>(
              stream: _firestore.collection('message').snapshots(),
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return const Center(
                    child: CircularProgressIndicator(),
                  );
                }
                final messages = snapshot.data!;
                List<Text> messageWidgets = [];
                for (var message in messages) // <== I get My error here
                   *// The type 'QuerySnapshot<Object?>' used in the 'for' loop must 
                   // implement Iterable.*
                  {
                  final messageText = message.data['text'];
                  final messageSender = message.data['sender'];
                  final messageWidget =
                      Text('$messageText from @$messageSender');
                  messageWidgets.add(messageWidget);
                }
                return Column(
                  children: messageWidgets,
                );
              },
            ),
  • 如果我从最终消息= snapshot.data;删除了Null检查操作员 我得到了一个不同的错误,这是

一个无效的表达式无法用作for-In循环中的迭代器。 尝试在将其用作迭代器之前检查值不是“ null”。

  • I encountered this problem while working with Flutter & Firebase (firestore), I did my research and couldn`t find anything helpful.

  • I want to turn my streams I get from firestore into a stream of widgets StreamBuilder().

  • Here is Firestore object initialization final FirebaseFirestore _firestore = FirebaseFirestore.instance;

  • Here is the method where I try to get my data from firestore as a stream

    void messagesStream() async {
      await for (var snapshot in _firestore.collection('message').snapshots()) {
        for (var message in snapshot.docs) {
          print(message.data());
        }
      }
    }
    
  • Here is StreamBuilder widget insdie my widget tree =>

StreamBuilder<QuerySnapshot>(
              stream: _firestore.collection('message').snapshots(),
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return const Center(
                    child: CircularProgressIndicator(),
                  );
                }
                final messages = snapshot.data!;
                List<Text> messageWidgets = [];
                for (var message in messages) // <== I get My error here
                   *// The type 'QuerySnapshot<Object?>' used in the 'for' loop must 
                   // implement Iterable.*
                  {
                  final messageText = message.data['text'];
                  final messageSender = message.data['sender'];
                  final messageWidget =
                      Text('$messageText from @$messageSender');
                  messageWidgets.add(messageWidget);
                }
                return Column(
                  children: messageWidgets,
                );
              },
            ),
  • If I removed the null check operator from final messages = snapshot.data;
    I get a different error which is

A nullable expression can't be used as an iterator in a for-in loop.
Try checking that the value isn't 'null' before using it as an iterator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎夕旧梦 2025-01-31 09:48:31

对于循环,轻松处理中的无效迭代的一种方法是使用??提供不可删除的后备值。例如:

List<int>? nullableList;
for (var element in nullableList ?? <int>[]) {
  ...
}

另外,您可以使用foreach使用条件 - 访问操作员:

List<int>? nullableList;
nullableList?.forEach((element) {
  ...
});

尽管通常我建议常规对于 foreach 循环。

One way to easily deal with nullable iterables in a for loop is to use ?? to provide a non-nullable fallback value. For example:

List<int>? nullableList;
for (var element in nullableList ?? <int>[]) {
  ...
}

Alternatively you can use the conditional-member-access operator with forEach:

List<int>? nullableList;
nullableList?.forEach((element) {
  ...
});

although generally I recommend normal for loops over forEach.

£噩梦荏苒 2025-01-31 09:48:31

我认为您可以检查“ snapshot.data”是否在其他任何事情之前都没有零。

// This basically means that if you have data, you show the widget CircularProgressIndicator()
if (snapshot.hasData) {
    return const Center(
        child: CircularProgressIndicator(),
    );
}

您可能会这样尝试:

// Means that if the snapshot hasn't data show the widget
if (!snapshot.hasData) {
    return const Center(
        child: CircularProgressIndicator(),
    );
}

I think you may check if "snapshot.data" is not null before anything else.

// This basically means that if you have data, you show the widget CircularProgressIndicator()
if (snapshot.hasData) {
    return const Center(
        child: CircularProgressIndicator(),
    );
}

You may try like this :

// Means that if the snapshot hasn't data show the widget
if (!snapshot.hasData) {
    return const Center(
        child: CircularProgressIndicator(),
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文