【问题标题】:QuerySnapshot filter null - FlutterQuerySnapshot 过滤器 null - Flutter
【发布时间】:2021-02-11 09:17:10
【问题描述】:

我正在尝试过滤有关颤振的查询,但它返回 null。

Future getChamados() async {
    final QuerySnapshot result = await Future.value(Firestore.instance
        .collection("chamados")
        .where("tid", isEqualTo: tid).orderBy('dateTime')
        .getDocuments());

    return result.documents;
  }

FutureBuilder

child: FutureBuilder(
              future: getChamados(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return new Center(
                    child: CircularProgressIndicator(),
                  );
                } else if (snapshot.data.length == 0 || tid == null || tid == "") {
                  return new Center(
                    child: Text(
                      'Nenhum Chamado.',
                      style: TextStyle(color: Colors.grey),
                    ),
                  );
                } else {
                  return ListView.builder(
                      shrinkWrap: true,
                      itemCount: snapshot.data.length,
                      itemBuilder: (context, index) {
                        return Card(
                            elevation: 8.0,
                            margin: new EdgeInsets.symmetric(
                                horizontal: 6.0, vertical: 5.0),
                            child: Container(
                              decoration:
                                  BoxDecoration(color: Color(0xFF00aacb)),
                              child: ListTile(
                                contentPadding: EdgeInsets.symmetric(
                                    horizontal: 15.0, vertical: 1.0),...

错误

在构建 FutureBuilder(dirty, state: _FutureBuilderState#2f75d) 时引发了以下 NoSuchMethodError: 在 null 上调用了 getter 'length'。 接收方:空 尝试调用:长度

【问题讨论】:

  • 发布 FutureBuilder 的代码。
  • 添加了未来的构建器。

标签: flutter google-cloud-firestore


【解决方案1】:

理想情况下,当 ConnectionState 为 ConnectionState.done 时,您应该访问 snapshot.data。当snapshot 为空时,您正在尝试访问.legnth

当 ConnectionState 为 ConnectionState.done 时,检查长度并有条件地渲染小部件。否则显示加载小部件。尝试像这样重构FutureBuilder

FutureBuilder(
  future: getChamados(),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) {
      return snapshot.data.length == 0 || tid == null || tid == ""
        ? ListView.builder(...)
        : Center(
            child: Text(
              'Nenhum Chamado.',
              style: TextStyle(color: Colors.grey),
            ),
          );
    }
    return CircularProgressIndicator();
  },
),

【讨论】:

  • 我按照您的指示更改了它并且您给出了这个错误:Listen for Query ( called where tid == 24082017 order by dateTime, name) failed: Status {code = FAILED_PRECONDITION, description = 查询需要索引。
  • 找到问题。需要在 Cloud Firestore 中创建索引。您可以通过错误本身生成的链接来创建它。
猜你喜欢
  • 2020-08-23
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
  • 2022-11-09
  • 2022-11-19
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多