1

I am having this issue of _JsonDocumentSnapshot While using streambuilder i don't know why i am running into this issue i saw other do the same thing and they did not run into this issue. My firestore data base structure=> Collection(Socails)=>Doc(AUTOID)=> 3fields + Collection(requests) =>Doc(AUTOID) 2Fields

import 'package:databse_web_test/database_services/getsocials.dart';
import 'package:databse_web_test/database_services/request.dart';
import 'package:databse_web_test/requestmodel.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:provider/provider.dart';

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

  @override
  _RequestManagerState createState() => _RequestManagerState();
}

class _RequestManagerState extends State<RequestManager> {
  @override
  Widget build(BuildContext context) {
    final requestsstream = FirebaseFirestore.instance
        .collection('socails')
        .doc('daaJgE8Pz5UQIlNh47UsmwWcqNi1')
        .collection('requests')
        .doc("AxTAKaXyeuC9Bql1lM7W")
        .snapshots();

    return StreamBuilder(
        stream: requestsstream,
        builder: (context, snapshot) {
          print(snapshot);
          Map<String, dynamic> data = snapshot.data! as Map<String, dynamic>;
          if (snapshot.hasData) {
            if (data['isApproved'] == false && data['isRequested'] == true) {
              return CircularProgressIndicator();
            } else if (data['isApproved'] == true &&
                data['isRequested'] == true) {
              return GetSocialData();
            }
          }
          return ElevatedButton(
              onPressed: () {
                SendRequest()
                    .updateUserData(isApproved: false, isRequested: true);
              },
              child: Text("Request Data"));
        });
  }
}

after using print(snapshot) it returns AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>>(ConnectionState.active, Instance of '_JsonDocumentSnapshot', null, null)

after print(snapshot.data) it returns _JsonDocumentSnapshot

and this the red screen of death message :- enter image description here

It's been 2 days im trying to solve this but i'm confuse cause im new to flutter

1 Answers1

1

Try this

return StreamBuilder(
        stream: requestsstream,
        builder: (context, snapshot) {
    print(snapshot);
      Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
          if (snapshot.hasData) {
            if (data['isApproved'] == false && data['isRequested'] == true) {
              return CircularProgressIndicator();
            } else if (data['isApproved'] == true &&
                data['isRequested'] == true) {
              return GetSocialData();
            }
          }
          return ElevatedButton(
              onPressed: () {
                SendRequest()
                    .updateUserData(isApproved: false, isRequested: true);
              },
              child: Text("Request Data"));
        });
Oreofe Solarin
  • 286
  • 5
  • 13
  • I am hiving this errors "A value of type 'Object?' can't be assigned to a variable of type 'QuerySnapshot'. Try changing the type of the variable, or casting the right-hand type to 'QuerySnapshot'." and "Error: A value of type 'String' can't be assigned to a variable of type 'int'. lib/request_manager.dart:33 if (data['isApproved'] == false && data['isRequested'] == true) { ^" – shriyash jagtap Dec 20 '21 at 07:38
  • @shriyashjagtap I have edited the answer, please recheck. – Oreofe Solarin Dec 20 '21 at 08:34
  • now i am getting this error The method 'data' isn't defined for the type 'Object'. Try correcting the name to the name of an existing method, or defining a method named 'data'. – shriyash jagtap Dec 20 '21 at 10:30