(Flutter Firestore)字段在DocumentSnapshotPlatform中不存在

qco9c6ql  于 2023-05-01  发布在  Flutter
关注(0)|答案(1)|浏览(107)

尝试从子集合中检索数据时出错。FireStore DB如下所示-〉用户;doc uid; messages;批准的贷款;贷款批准。
我已经仔细检查了我输入的文档和集合是否正确,那么我遗漏了什么?
任何建议将不胜感激。
谢谢你

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'];
                });
          },
        ));
  }
}
2mbi3lxu

2mbi3lxu1#

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

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

根据您的屏幕截图,此Messages集合中有一个Loans approved文档没有任何数据。此文档有一个Loans approved子集合,该子集合又包含一个Loans approved文档。正是这个文档包含字段(InterestLoan amount等)。).
因此,如下定义您的流应该可以做到这一点:

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

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

相关问题