尝试从子集合中检索数据时出错。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'];
});
},
));
}
}
1条答案
按热度按时间2mbi3lxu1#
原因是您将Stream(您的
db
变量)定义为根据您的屏幕截图,此
Messages
集合中有一个Loans approved
文档没有任何数据。此文档有一个Loans approved
子集合,该子集合又包含一个Loans approved
文档。正是这个文档包含字段(Interest
、Loan amount
等)。).因此,如下定义您的流应该可以做到这一点:
PS:我建议你在写集合、文档和字段名时使用camelCase convention。