在这里,我正在使用List<DocumentSnapshot>
信息流,对此信息我不太了解。我认为我们可以在Flutter中使用流进行分页,因为它会加载数据更改。我以为一切都按预期进行,直到调试时才显示异常,该异常表明该流被调用为null,我不知道出错的地方,也不知道代码是正确的,但是该代码是针对何时用户滚动到末尾,将抓取新列表并将其添加到流中。根据我的错误,我已将其置于init状态,以确保流不为null,这也会给我错误
class _Articles extends StatefulWidget {
@override
_ArticlesState createState() {
return new _ArticlesState();
}
}
class _ArticlesState extends State<_Articles> {
final firestore = Firestore.instance;
ScrollController controller;
StreamController<List<DocumentSnapshot>> list;
String s;
@override
void initState() {
controller = ScrollController()..addListener(_listen);
addlist(); // this is to enure the stream should not be null
super.initState();
}
@override
void dispose() {
list.close();
controller.removeListener(_listen);
super.dispose();
}
_listen() { // to check user end of the list or not
if (controller.offset >= controller.position.maxScrollExtent &&
!controller.position.outOfRange) {
listadd(); // if end of the list add to stream
}
}
@override
Widget build(BuildContext context) {
var height = MediaQuery.of(context).size.height;
var width = MediaQuery.of(context).size.width;
return Container(
padding: EdgeInsets.only(top: 10, bottom: 10),
color: Colors.grey[100],
child: StreamBuilder<List<DocumentSnapshot>>(
stream: list.stream,
builder: (context, snapshot) {
return //some widget like listview builder with data in snapshot
}),
);
}
void addlist() async{
QuerySnapshot list1= await Firestore.instance.collection('articles').limit(3).orderBy('title').getDocuments();
list.add(list1.documents);
s= list1.documents[2].data['title'];
}
void listadd() async{
QuerySnapshot li= await Firestore.instance.collection('articles').orderBy('title').limit(3).startAt([s]).getDocuments();
list.add(li.documents);
s=li.documents[2].data['title'];
}
}
以上是我的代码不知道如何流式传输错误和线索,如果代码本身是错误的,请纠正我任何帮助。
答案 0 :(得分:0)
list
似乎没有正确初始化,因为 addlist() 没有作为异步运行,即 addlist().whenComplete()
。下面的代码段符合 cloud_firestore 2.2.0
您在这里可以做的只是将 QuerySnapshot 添加为流。
child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('articles')
.orderBy('title')
.limit(3)
.snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
return //some widget like listview builder with data in snapshot
},
),