Here I am using a stream of List<DocumentSnapshot> I don't know much about my stream. I thought we can use streams for pagination in Flutter as it loads with change in data. I thought every thing went as expected until debugging when I debug it show an exception says that the stream was called on null and I don't know where it went wrong also don't know the code is correct but the code is for when the user scroll to the end new list is grabbed and added to stream. According to my error I have placed init state to ensure that stream is not null also it gives me the error
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'];
}
}
above is my code don't know how to stream this just a trail and error that if the code itself is wrong please correct me any sort of help is appreciated.
listfield is never initialized. Try replacing the current uninitialized field declaration withfinal list = StreamController<List<DocumentSnapshot>>();