name__
name__

Reputation: 79

Class 'DocumentSnapshot' has no instance getter 'doc'. Receiver: Instance of 'DocumentSnapshot' Tried calling: doc

I recently got started with firebase firestore and this is one error I cant seem to find a answer to, I tried writing snapshot.data.documents as docs, doc and document after looking at answers for similar questions but it is still throwing the same error. what do I do? firestore version I am using is cloud_firestore: ^1.0.7

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
// import 'package:vola1/colors.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';

class test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
        body: StreamBuilder(
      stream: FirebaseFirestore.instance
          .collection('countries')
          .doc('nW9L4LGpn2MZVyiTyUII')
          .snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) return Text('Loading data.. please wait..');
        return Column(
          children: <Widget>[
            Text(
              snapshot.data.doc[0]['name'],
              style: TextStyle(fontSize: 20),
            ),
          ],
        );
      },
    ));
  }
}

the exception it is throwing

======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building StreamBuilder<DocumentSnapshot>(dirty, state: _StreamBuilderBaseState<DocumentSnapshot, AsyncSnapshot<DocumentSnapshot>>#af0fe):
Class 'DocumentSnapshot' has no instance getter 'documents'.
Receiver: Instance of 'DocumentSnapshot'
Tried calling: documents

The relevant error-causing widget was: 
  StreamBuilder<DocumentSnapshot> file:///D:/flutter%20course/vola1/lib/test.dart:14:15
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      test.build.<anonymous closure> (package:vola1/test.dart:24:29)
#2      StreamBuilder.build (package:flutter/src/widgets/async.dart:545:81)
#3      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48)
#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27)
...
====================================================================================================

Upvotes: 1

Views: 747

Answers (1)

Victor Eronmosele
Victor Eronmosele

Reputation: 7716

To get the data from a DocumentSnapshot, use the data getter which retrieves all fields in the document as a Map; docs is for the QuerySnapshot class.

Change this code below:

          snapshot.data.doc[0]['name'],

to this:

          snapshot.data['name'],

Upvotes: 2

Related Questions