我们这里有一个屏幕应该从firestore云获取数据,但抛出这个错误,即使我确保检查每一个可能的空值,但没有解决这个错误。
看起来我遗漏了一些找不到的内容(值)错误可能位于ShowMessages类中
下面是代码:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class GroupScreen extends StatelessWidget {
final String groupUID;
final List members;
final bool admin;
final String title;
final String username;
const GroupScreen({
super.key,
required this.groupUID,
required this.members,
required this.admin,
required this.title,
required this.username
});
@override
Widget build(BuildContext context) {
TextEditingController _msg = TextEditingController();
return Scaffold(
backgroundColor: Colors.deepPurple[50],
appBar: AppBar(
title: Text(title),
backgroundColor: Colors.deepPurple[300],),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text('Messages'),
ShowMessages(groupUID: groupUID,),
Row(
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(color: Colors.deepPurpleAccent),
)
),
child: TextField(
controller: _msg,
decoration: InputDecoration(hintText: 'Enter Message...',border: InputBorder.none),
),
)
),
IconButton(onPressed: (){
if(!_msg.text.isEmpty) {
FirebaseFirestore.instance.collection("users").
doc(FirebaseAuth.instance.currentUser!.uid).
collection("groups").
doc(groupUID).
collection("chat").
doc().
set({
"msg": _msg.text,
"user":username
});
_msg.clear();
}
}, icon: Icon(Icons.send, color: Colors.deepPurple,))
],
),
],
),
);
}
}
class ShowMessages extends StatelessWidget {
final String groupUID;
const ShowMessages({
super.key,
required this.groupUID
});
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseFirestore.instance.collection("users").doc(FirebaseAuth.instance.currentUser!.uid).collection("groups").doc(groupUID).collection("chat").doc().snapshots(),
builder: (context,snapshot){
if (snapshot.hasData){
return ListView.builder(
shrinkWrap: true,
primary: true,
itemCount: snapshot.data!.data()!.length != null ? snapshot.data!.data()!.length : 0,
itemBuilder: (context,index){
QueryDocumentSnapshot value = snapshot.data!.data()![index] != null ? snapshot.data!.data()![index] : null ;
return ListTile(
title: Text(value != null ? value['msg'] : ''),
);
},
);
}
return Text('');
} ,
);
}
}
1条答案
按热度按时间66bbxpm51#
在访问firebase用户对象之前,请执行null检查。另外,如果您使用vscode作为代码编辑器,请启用未捕获异常和所有异常选项。这将帮助您找到发生错误的确切行。