我正在使用Firebase的后端支持在混乱中创建一个Todo应用程序,一切正常,但是当我包含DocumentSnapshot时,这给了我错误。我找不到解决此问题的方法。
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 285 pos 10: 'data != null'
after the line included
documentSnapshot["todoTitle"]
我该如何解决?
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class todoUi extends StatefulWidget {
@override
_todoUiState createState() => _todoUiState();
}
class _todoUiState extends State<todoUi> {
List todos = List();
String input = "";
createTodos() {
DocumentReference documentReference =
Firestore.instance.collection("MyTodos").document(input);
//Maping
Map<String, String> todos = {"todosTitle": input};
documentReference.setData(todos).whenComplete(() {
print("$input created");
});
}
deleteTodos() {}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sharjeel's TODO LIST"),
centerTitle: true,
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.purple,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
title: Text("Add Your Task"),
content: TextField(
onChanged: (String value) {
input = value;
},
),
actions: <Widget>[
FlatButton(
onPressed: () {
createTodos();
Navigator.of(context).pop();
},
child: Text("Add"))
],
);
});
},
child: Icon(
Icons.add,
color: Colors.white,
),
),
body: StreamBuilder(
stream: Firestore.instance.collection("MyTodos").snapshots(),
builder: (context, snapshots) {
return ListView.builder(
shrinkWrap: true,
itemCount: snapshots.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot documentSnapshot =
snapshots.data.documents[index];
return Dismissible(
key: Key(index.toString()),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16)),
margin: EdgeInsets.all(8),
child: ListTile(
title: Text(
documentSnapshot["todoTitle"]
),
trailing: IconButton(
icon: Icon(
Icons.delete,
color: Colors.red,
),
onPressed: () {
setState(() {
todos.removeAt(index);
});
})),
),
);
});
}),
);
}
}
答案 0 :(得分:1)
我猜这是因为第一次挂载小部件时,还没有从Stream
获取数据。
也许尝试:
Text(documentSnapshot["todoTitle"] ?? "")