2

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();
  }
}

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Laurent Thomas
  • 232
  • 4
  • 24

2 Answers2

2

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').

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 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" – Laurent Thomas Jun 07 '21 at 04:56
1

First of all, please try to implement ListView for

if(snapshot.hasData)