【问题标题】:Show certain data with Flutter and Firestore使用 Flutter 和 Firestore 显示某些数据
【发布时间】:2018-10-22 13:54:56
【问题描述】:

我目前正在使用此代码来映射整个待办事项列表:

return new StreamBuilder<QuerySnapshot>(
  stream: Firestore.instance.collection('todo_list').snapshots,
  builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
    if (!snapshot.hasData) return new Text('Loading...');
    return new ListView(
      children: snapshot.data.documents.map((DocumentSnapshot document) {
        document['status'] == true ?
        new ListTile(
          title: new Row(
            children: <Widget>[
              new Expanded(
                child: new Text(document['task'], 
                style: new TextStyle(
                  decoration: TextDecoration.lineThrough,
                  ),
                ),
              )
            ],
          )
        ) : new Text("");
      }).toList(),
    );
  },
);

我想显示状态为真的任务。但是,这样做时会出现错误:

I/flutter (21800): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter (21800): The following assertion was thrown during performLayout():
I/flutter (21800): 'package:flutter/src/widgets/sliver.dart': Failed assertion: line 291 pos 12: 'child != null': is
I/flutter (21800): not true.
I/flutter (21800):
I/flutter (21800): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (21800): more information in this error message to help you determine and fix the underlying cause.
I/flutter (21800): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (21800):   https://github.com/flutter/flutter/issues/new
I/flutter (21800):
I/flutter (21800): When the exception was thrown, this was the stack:
I/flutter (21800): #2      SliverChildListDelegate.build (package:flutter/src/widgets/sliver.dart)
I/flutter (21800): #3      SliverMultiBoxAdaptorElement._build.<anonymous closure> (package:flutter/src/widgets/sliver.dart:716:67)
I/flutter (21800): #4      _HashMap.putIfAbsent 

...

有什么建议吗?

【问题讨论】:

  • 你能发布实际的错误吗?
  • 完成。如果您需要整个代码,hmu。错误比这长得多。

标签: google-cloud-firestore flutter


【解决方案1】:

您忘记返回三元的实际结果。

return  document['status'] == true ?
    ....

这应该可以解决问题

【讨论】:

  • 非常感谢!
【解决方案2】:

我找到了解决办法:

Firestore.instance.collection('todo_list').where('status', isEqualTo: false).snapshots,

您可以使用 Firestore 代码进行查询。

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 2020-04-26
    • 2020-01-14
    • 2021-01-21
    • 2021-11-28
    • 2019-05-03
    • 2023-02-10
    • 2020-08-28
    • 2012-08-18
    相关资源
    最近更新 更多