Home > Back-end >  Instance of '_JsonQueryDocumentSnapshot'
Instance of '_JsonQueryDocumentSnapshot'

Time:07-21

main screen code :

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:day_event_viewer/services/firestore.dart';
import 'package:flutter/material.dart';

class TodayEventScreen extends StatefulWidget {
  @override
  _TodayEventScreenState createState() => _TodayEventScreenState();
}

class _TodayEventScreenState extends State<TodayEventScreen> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: StreamBuilder(
          stream: getUserDatas(),
          builder:
              (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (!snapshot.hasData) {
              return const Text('loading data');
            } else if (snapshot.hasData) {
              return ListView.builder(
                itemCount: snapshot.data!.docs.length,
                itemBuilder: (BuildContext context, int index) {
                  return Text(snapshot.data!.docs[index].toString());
                },
              );
            }
            return const Text('somethng\'s wrong');
          }),
    );
  }
}

stream getUserDatas() code :

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:day_event_viewer/screens/add_screen.dart';

Stream<QuerySnapshot> getUserDatas() async* {
  final uid = await getUid();
  yield* FirebaseFirestore.instance
      .collection('usersdatas')
      .doc(uid)
      .collection('profile')
      .snapshots();
}

it is giving me "Instance of '_JsonQueryDocumentSnapshot'" in my screen. what is this and how can I get the data in firestore. I'm new to firebase so anyone's help would be appreciated.

CodePudding user response:

you are trying to display instance not the actual value you want,you should try to display return Text(snapshot.data!.docs[index].here the data you want to display.toString());

CodePudding user response:

When you load data from Firestore, you get a DocumentSnapshot for each document. Since you're calling toString() on that here, you see the debug representation of the object in your UI:

itemBuilder: (BuildContext context, int index) {
  return Text(snapshot.data!.docs[index].toString());
},

What you probably want is to get one or more of the fields from the document's data, and display that. Say that your document contains a field called message, you can display that value with:

itemBuilder: (BuildContext context, int index) {
  var doc = snapshot.data!.docs[index];
  var data = doc.data() as Map;
  return Text(data['message']);
},

I recommend also having a look at the documentation on showing realtime results from Firestore in Flutter as it has a quite elaborate example.

  • Related