2

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.

1
  • It looks like the list field is never initialized. Try replacing the current uninitialized field declaration with final list = StreamController<List<DocumentSnapshot>>(); Commented Feb 21, 2019 at 17:13

1 Answer 1

1

list doesn't seem to be initialized properly since addlist() wasn't run as an async i.e. addlist().whenComplete(). The snippet below conforms to cloud_firestore 2.2.0

What you can do here is just add the QuerySnapshot as a stream.

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
  },
),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.