firebase - 颤动的火力基地中的snapshot.data为空
问题描述
我的小部件树卡在流构建器条件上,即 !snapshot.hasdata 所以快照数据为 null 。我签入了firestore,但是有数据,所以firebase文件中是否有任何错误,因为没有显示数据。即使我添加了数据,如何摆脱这个问题我希望这个问题也能在未来对其他人有所帮助
class TodoList extends StatefulWidget {
@override
State<TodoList> createState() => _TodoListState();
}
class _TodoListState extends State<TodoList> {
TextEditingController todoTitleController = TextEditingController();
bool iscomplete = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: primarycolor,
body: SafeArea(
child: StreamBuilder<List<Todolist>>(
stream: DatabaseService().listTodo(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Loading();
}
print(snapshot.data![0].title);
List<Todolist> todos = snapshot.data;
return Padding(
padding: EdgeInsets.all(25.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// ignore: prefer_const_constructors
Center(
child: const Text(
'All Todos',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
// ignore: prefer_const_constructors
Divider(),
// ignore: prefer_const_constructors
SizedBox(height: 20),
ListView.separated(
separatorBuilder: (context, index) => Divider(
color: Colors.grey[800],
),
shrinkWrap: true,
itemCount: todos!.length,
itemBuilder: (context, index) {
return Dismissible(
key: Key(todos![index].title),
background: Container(
padding: EdgeInsets.only(left: 20),
alignment: Alignment.centerLeft,
child: Icon(Icons.delete),
color: Colors.red,
),
onDismissed: (direction) async {
await DatabaseService()
.removeTodo(todos![index].id);
},
child: ListTile(
onTap: () {
setState(() {
iscomplete = !iscomplete;
});
},
leading: Container(
padding: EdgeInsets.all(4),
height: 43,
width: 33,
decoration: BoxDecoration(
color: secondarycolor,
shape: BoxShape.circle,
),
child: iscomplete
? Icon(
Icons.check,
color: Colors.grey[200],
)
: Container(),
),
title: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
todos![index].title,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w600,
color: Colors.grey[200]),
),
这是我的firebase文件:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:todolist/models/model.dart';
class DatabaseService {
CollectionReference todosCollection =
FirebaseFirestore.instance.collection("Todos");
Future createNewTodo(String title) async {
return await todosCollection.add({
"title": title,
"iscomplete": false,
});
}
Future completTask(id) async {
await todosCollection.doc(id).update({"iscomplete": true});
}
Future removeTodo(uid) async {
await todosCollection.doc(uid).delete();
}
List<Todolist> todoFromFirestore(QuerySnapshot snapshot) {
if (snapshot != null) {
return snapshot.docs.map((e) {
return Todolist(
iscomplete: e["iscomplete"],
title: e["title"],
id: e.id,
);
}).toList();
} else {
return [];
}
}
Stream<List<Todolist>> listTodo() {
return todosCollection.snapshots().map(todoFromFirestore);
}
}
解决方案
这是因为snapshot
有连接状态
StreamBuilder(
stream: stream,
builder: (context, snapshot) {
// Check for errors
if (snapshot.hasError) {
return SomethingWentWrong();
}
// Once complete, show your application
if (snapshot.connectionState == ConnectionState.done) {
// here you should do your worj
}
// Send updates to the widget
if (snapshot.connectionState == ConnectionState.active) {
// or here if you take data in pages
}
// Otherwise, show something whilst waiting for initialization to complete
return LoadingWidget();
},
);
推荐阅读
- node.js - 有一种方法可以在 MongoDB 中强制执行引用完整性
- android - 如何在 android studio 设备模拟器中模拟来自“不可用”的电话?
- sql - 如何访问大查询表中列内的数据数组的列名?
- java - java.lang.ExceptionInInitializerError 引起:com.fasterxml.jackson.databind.JsonMappingException:Jackson 版本太旧 2.5.1
- r - 根据总和选择列
- python - 批量更新的 Welford 方差/标准算法的公式是什么?
- google-apps-script - 在两个电子表格之间查找一行,然后查找包含第一行中的值的行
- java - 如何在 JFROG 上上传带有目录的 jar 文件?
- android - 使用 addImage() 和会话优化 ArCore Fragment 导航
- python - UnboundLocalError:分配前引用的局部变量“var”