【问题标题】:(Flutter Firestore) Field does not exist within DocumentSnapshotPlatform(Flutter Firestore) DocumentSnapshotPlatform 中不存在字段
【发布时间】:2021-09-29 13:25:40
【问题描述】:

我在尝试从子集合中检索数据时遇到错误。 firestore db如下-> users;文档 uid;消息;批准的贷款;贷款获批。

我仔细检查了我输入的文档和集合是否正确,所以我错过了什么?

任何建议将不胜感激。

谢谢

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class MessagesScreen extends StatefulWidget {
  @override
  MessagesScreenState createState() => MessagesScreenState();
}

FirebaseAuth _auth = FirebaseAuth.instance;
final uid = _auth.currentUser!.uid;
var boldFont = TextStyle(fontFamily: 'Inter', fontWeight: FontWeight.w600);

class MessagesScreenState extends State<MessagesScreen> {
  final db = FirebaseFirestore.instance
      .collection('users')
      .doc(uid)
      .collection('Messages')
      .snapshots();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          titleSpacing: 30,
          automaticallyImplyLeading: false,
          backgroundColor: Color.fromRGBO(1, 67, 55, 1),
          toolbarHeight: 100,
          title: new Text(
            'Messages',
            style: TextStyle(
                color: Color.fromRGBO(255, 255, 255, 1),
                fontFamily: 'Poppins',
                fontSize: 25,
                letterSpacing: 1.2,
                fontWeight: FontWeight.bold,
                height: 1),
          ),
        ),
        body: StreamBuilder<QuerySnapshot>(
          stream: db,
          builder: (context, snapshot) {
            if (!snapshot.hasData)
              return const Center(
                child: CircularProgressIndicator(),
              );
            return ListView.builder(
                itemCount: snapshot.data!.docs.length,
                itemBuilder: (BuildContext context, int index) {
                  return snapshot.data!.docs[index]['Loans approved']
                      ['Loans overdue']['Loans paid'];
                });
          },
        ));
  }
}

【问题讨论】:

  • 请查看我最近的回答here。基本上字段数据将在docs[index].data()!.
  • 当我这样做时,我收到此错误:未为“对象”类型定义运算符“[已批准的贷款]”。尝试定义运算符'[]'。
  • 您似乎有一个 fromJson 转换器,因此 is 已经是这里的一个对象。打印出来或用断点停止,然后检查 docs[index].data()! 中的内容。
  • 抱歉,我是 Flutter 的初学者,所以不知道该怎么做?

标签: flutter google-cloud-firestore


【解决方案1】:

原因是您将 Stream(您的 db 变量)定义为

FirebaseFirestore.instance
      .collection('users')
      .doc(uid)
      .collection('Messages')
      .snapshots();

根据您的屏幕截图,此Messages 集合中有一个Loans approved 文档没有任何数据。该文档有一个Loans approved 子集合,而该子集合又包含一个Loans approved 文档。正是此文档包含字段(InterestLoan amount 等)。

因此,如下定义您的 Stream 应该可以解决问题:

FirebaseFirestore.instance
      .collection('users')
      .doc(uid)
      .collection('Messages')
      .doc('Loans approved')
      .collection('Loans approved')
      .snapshots();

PS:我建议您在编写集合、文档和字段名称时使用camelCase convention

【讨论】:

  • 错误消失了,但现在我得到一个空白屏幕,在圆圈进度指示器触发后没有返回任何数据
  • 您想获得哪个确切的字段值?在您的代码中,我们可以看到['Loans approved']['Loans overdue']['Loans paid'],但这些似乎都不是文档的字段(至少从我们在您的屏幕截图中可以看到)。尝试做return snapshot.data!.docs[index]['Loan amount'];
  • 好的,我刚刚尝试返回snapshot.data!.docs[index]['Loan amount'];,但仍然出现空白屏幕。圆形进度指示器仍在触发,然后没有加载。
  • 作为一个调试练习,你能不能尝试实现我们在文档中找到的example,即return ListView(children: snapshot.data!.docs.map((DocumentSnapshot document) {Map&lt;String, dynamic&gt; data = document.data()! as Map&lt;String, dynamic&gt;;...
猜你喜欢
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
  • 2021-05-16
  • 2021-03-05
  • 2021-10-26
  • 2021-10-19
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多