Search code examples
firebasefluttergoogle-cloud-firestoreflutter-streambuilder

How to get user single detail from firestrome using streambuilder in flutter


I am trying to build an app using Flutter and Firestore. When loading current user single filed using StreamBuilder to display it in a Text, I get the following error

════════ Exception caught by widgets library ═══════════════════════════════════ The following StateError was thrown building StreamBuilder<DocumentSnapshot<Object?>>(dirty, state: _StreamBuilderBaseState<DocumentSnapshot<Object?>, AsyncSnapshot<DocumentSnapshot<Object?>>>#8a26d): Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist.

Here is my code

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class Status extends StatefulWidget {
  Status({Key? key}) : super(key: key);
  @override
  State<Status> createState() => _StatusState();
}

class _StatusState extends State<Status> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<DocumentSnapshot>(
        stream: FirebaseFirestore.instance
            .collection("Users")
            .doc(FirebaseAuth.instance.currentUser!.uid)
            .snapshots(),
        builder: (context, AsyncSnapshot snapshot) {
          if (!snapshot.hasData) {
            return Text("Loading");
          } else if (snapshot.hasError) {
            return Text('Something went wrong');
          } else if (snapshot.connectionState == ConnectionState.waiting) {
            return CircularProgressIndicator();
          }
          dynamic data = snapshot.data;
          return new Text(data['status']);
        });
  }
}

Solution

  • You need to pass

    DocumentSnapshot to AsyncSnapshot.. Try this code

    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:flutter/material.dart';
    
     class Status extends StatefulWidget {
       Status({Key? key}) : super(key: key);
      @override
       State<Status> createState() => _StatusState();
     }
    
     class _StatusState extends State<Status> {
    
     final Stream<DocumentSnapshot> _usersStream = FirebaseFirestore.instance
      .collection("Users")
      .doc(FirebaseAuth.instance.currentUser.uid)
      .snapshots()
    
     @override
     Widget build(BuildContext context) {
      return StreamBuilder<DocumentSnapshot>(
         stream: _usersStream,
          builder: (context,  AsyncSnapshot<DocumentSnapshot> snapshot) {
           if (!snapshot.hasData) {
            return Text("Loading");
          } else if (snapshot.hasError) {
            return Text('Something went wrong');
          } else if (snapshot.connectionState == ConnectionState.waiting) {
            return CircularProgressIndicator();
          }
          dynamic data = snapshot.data;
          return new Text(data['status']);
        });
      }
     }