Home > Enterprise >  my Querysnapshot Map String become AsyncSnapshot<Object> with no reason
my Querysnapshot Map String become AsyncSnapshot<Object> with no reason

Time:01-14

I have a streamBuilder where the "snapshots()" change from Stream<QuerySnapshot<Map<String, dynamic>>> to AsyncSnapshot<Object?>. This is weird because I have another app, with identical code and works fine. The problem is, I cannot access "snapshot.data.docs" and all firestore datas. Why is happening?

my code:

 body: StreamBuilder(
             //Stream<QuerySnapshot<Map<String, dynamic>>>
      stream: firestore.collection('Books').orderBy('name').snapshots(),

                 //AsyncSnapshot<Object?>
      builder:  (context, snapshot ) {
        if (snapshot.connectionState == ConnectionState.waiting)
          return Center(child: CircularProgressIndicator());
        {
          final document = snapshot.data?; //error here
          return ListView.builder(
              itemCount: document?.length,
              itemBuilder: (context, index) {
                return Column(

CodePudding user response:

Declare your StreamBuilder Widget response type as a dynamic, and you should use

snapshot.data.docs without any problem.

 body: StreamBuilder<dynamic>(
 stream: FirebaseFirestore.instance.collection('Books').orderBy('name').snapshots(),

 builder: (context, snapshots) {
      if(snapshots.hasData){
         print(snapshots.data.docs.toString());
      }});
  • Related