[SOLVED] How to fix Bad state: field does not exist within the DocumentSnapshotPlatform Firebase Flutter StreamBuilder

Issue

This Content is from Stack Overflow. Question asked by Tishta Biswas


import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:treazer/screen/chat.dart';
import 'package:treazer/screen/notifi.dart';
// import 'package:http/http.dart' as http;
// import 'dart:convert';
// import 'dart:async';

class MyHome extends StatefulWidget {
  MyHome({Key? key}) : super(key: key);

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  @override
  Widget build(BuildContext context) {
//Firebase code
    final CollectionReference _post =
        FirebaseFirestore.instance.collection('post');
        

//firebase add update delete functioin

/*
await _Post.add({"name": name, "price": price});
await _Post.update({"name": name, "price": price});
await _Post.doc(productId) .delete();
*/

    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(
          color: Colors.lightBlueAccent,
          size: 20,
        ),
        title: Text("Treazer"),
        actions: [
          IconButton(onPressed: () {}, icon: Icon(Icons.search)),
          IconButton(
              onPressed: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => Chat(),
                    ));
              },
              icon: Icon(Icons.chat_rounded)),
          IconButton(
              onPressed: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => Notifi(),
                    ));
              },
              icon: Icon(Icons.notifications)),
        ],
        elevation: 0,
        backgroundColor: Colors.white,
        foregroundColor: Colors.lightBlueAccent,
      ),
      body: StreamBuilder(
          stream: _post.snapshots(),
          builder: (BuildContext context,
              AsyncSnapshot<QuerySnapshot> streamSnapshoot) {
            if (streamSnapshoot.hasData) {
              return ListView.builder(
                itemCount: streamSnapshoot.data!.docs.length,
                itemBuilder: (context, index) {
                  final DocumentSnapshot documentSnapshot =
                      streamSnapshoot.data!.docs[index];
                  return Container(
                    
                    
                    margin: EdgeInsets.all(30),
                    padding: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                        color: Colors.white,
                       
                        borderRadius: BorderRadius.all(Radius.circular(10.0)),
                        boxShadow: [
                          BoxShadow(
                            color: Color.fromARGB(255, 191, 219, 254),
                            blurRadius: 20.0,
                          ),
                        ]),
                    child: ListTile(
                      title: Text(documentSnapshot['name']),
                      subtitle: Text(documentSnapshot['price'].toString()),
                    ),
                  
                    
                  );

                  // Card(
                  //   margin: EdgeInsets.all(10),
                  //   child: ListTile(
                  //     title: Text(documentSnapshot['name']),
                  //     subtitle: Text(documentSnapshot['price'].toString()),
                  //   ),
                  // );
                },
              );
            }
            return Center(
              child: CircularProgressIndicator(),
            );
          }),
    );
  }
}

‘Error shows the field documentSnapshot field.how can I resolve this issue. it throughout the red screen.How to fix Bad state: field does not exist within the DocumentSnapshotPlatform Firebase Flutter StreamBuilder. How to fix Bad state: field does not exist within the DocumentSnapshotPlatform Firebase Flutter StreamBuilder, How to fix Bad state: field does not exist within the DocumentSnapshotPlatform Firebase Flutter StreamBuilder’

Solution

Change your factory to this:

factory User.fromDocument(DocumentSnapshot doc) {
  return User(
    id: doc.data()['id'],
    email: doc.data()['email'],
    username: doc.data()['username'],
    photoUrl: doc.data()['photoUrl'],
    displayName: doc.data()['displayName'],
    bio: doc.data()['bio'],      
    fullNames: doc.data()['fullNames'],
    practice: doc.data()['practice'],
    speciality: doc.data()['speciality'],
    phone: doc.data()['phone'],
    mobile: doc.data()['mobile'],
    emergency: doc.data()['emergency'],
    address: doc.data()['address'],
    city: doc.data()['city'],
    location: doc.data()['location'],
  );
}

Answered by Rishi Malgwa , edited by Jeremy Caney


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 4.0.

people found this article helpful. What about you?