ListView & Snapshot - error - Bad state: field does not exist within the DocumentSnapshotPlatform

1,014

In this code:

return new ListView(
  children: snapshot.data.docs.map((prgSnapshot){
    return Card(
        child: ListTile(
          leading: CircleAvatar(

          ),
          title: Text(prgSnapshot['project_Name']),
        )
    ) ;
  }).toList(),
);

The snapshot.data is a QuerySnapshot, which means that prgSnapshot is a DocumentSnapshot. If you check that documentation, you'll see that it doesn't have an [] accessor.

If you want to get a field from the data of the document, use prgSnapshot.data()['project_Name'] or prgSnapshot.get('project_Name').

Share:
1,014
Laurent Thomas
Author by

Laurent Thomas

After many years without developing, I have decided to learn Dart.

Updated on December 20, 2022

Comments

  • Laurent Thomas
    Laurent Thomas over 1 year

    I have a little issue with a snapshot and a ListView. So far it was working very well. But since I have updated flutter and Dart, I am getting an error.

    The following StateError was thrown building StreamBuilder<QuerySnapshot<Object?>>(dirty, state: _StreamBuilderBaseState<QuerySnapshot<Object?>, AsyncSnapshot<QuerySnapshot<Object?>>>#8b4ae): Bad state: field does not exist within the DocumentSnapshotPlatform

    I do not understand how to fix the problem. I have checked the name of the fields in the document and it is fine. Please, can you help me to understand this? It would be appreciated. Thank you

    import 'package:flutter/material.dart';
    import 'package:flutter/cupertino.dart';
    
    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:cloud_firestore/cloud_firestore.dart';
    
    class ProjectsList3 extends StatefulWidget {
      ProjectsList3 ({Key key}) : super(key : key);
    
      @override
      _ProjectsList3State createState() => _ProjectsList3State();
    }
    
    class _ProjectsList3State extends State<ProjectsList3> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          drawer:  new MyMenu(),
          appBar: new AppBar(
            title: new Text('Projects'),
          ),
          body: Column(
            children: [
              Expanded(
                child: StreamBuilder<QuerySnapshot>(
                    stream: FirebaseFirestore.instance
                        .collection('Users')
                        .doc(FirebaseAuth.instance.currentUser.uid)
                        .collection('projects')
                        .snapshots(),
                    builder: (BuildContext context,
                        AsyncSnapshot<QuerySnapshot> snapshot) {
                      if(snapshot.connectionState == ConnectionState.waiting){
                        return Center(child: LinearProgressIndicator());
                      }
                      else{
                        return new ListView(
                          children: snapshot.data.docs.map((prgSnapshot){
                            return Card(
                                child: ListTile(
                                  leading: CircleAvatar(
    
                                  ),
                                  title: Text(prgSnapshot['project_Name']),
                                )
                            ) ;
                          }).toList(),
                        );
                        //if (!snapshot.hasData) {
                        // return Center(
                        //   child: CircularProgressIndicator(),
                        // );
                      }
                    }
                ),
              ),
            ],
          ),
          bottomNavigationBar: MyBottomAppBar(),
        );
        throw UnimplementedError();
      }
    }
    
    
    • Kevin Quinzel
      Kevin Quinzel almost 3 years
      Does this answer your issue?
    • Laurent Thomas
      Laurent Thomas almost 3 years
      I will try and let you know. Thank you
  • Laurent Thomas
    Laurent Thomas almost 3 years
    Thank you. I have tried your recommendation but I am still getting the same error message "Bad state: field does not exist within the DocumentSnapshotPlatform"