Class '_JsonQuerySnapshot' has no instance getter 'documents'

Viewed 129

Please I'm trying to make a quote app using an Image Url and quotes gotten from cloud firestore. I have already made a collection of "quotes" with documents that have the field "quote" and "author" now I'm trying to access the collection from firestore but I keep having this issue. My plan is when the user double-taps the screen it gets a new random image plus the next document in the "quotes" collection. Here is my code below. My cloud firestore is shown in the image below.

import 'dart:async';
import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart';
import 'package:cloud_firestore/cloud_firestore.dart';


class MyQuoteApp extends StatefulWidget {
  // MyQuoteApp({Key  key, this.title}) : super(key: key);
  final String title;
  MyQuoteApp({required this.title});

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

class _MyQuoteAppState extends State<MyQuoteApp> {

  // String _url = "https://api.quotable.io/random";
  String _imageUrl = "https://source.unsplash.com/random/";
late  StreamController _streamController;
  late Response response;
  int counter = 0;
    final FirebaseFirestore _firestore= FirebaseFirestore.instance;
    late Stream _stream= _firestore.collection("quotes").snapshots();
    dynamic quoting;
 



  
  void _newImage() {
    setState(() {
      _imageUrl = 'https://source.unsplash.com/random/$counter';
      counter++;
    });
  }

  getQuotes() async {
    _newImage();
    _streamController.add("waiting");
    // ??
    quoting= _firestore.collection("quotes").snapshots();
    response = await quoting.get();
 
    // response = await get (Uri.parse (_stream.toString()));
        // response = await get (_stream);

    _streamController.add(json.decode(response.body ));
        // _streamController.add(_stream as dynamic);
        // _streamController.addStream(_stream);

  }

  @override
  void initState() {
    super.initState();
    // _streamController = StreamController();
    // _stream = _streamController.stream;
    getQuotes();
  }

  

  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
    return Scaffold(

      backgroundColor: Colors.black26,
        appBar: AppBar(
          title: Center(child: Text(widget.title)),
        ),
        body: Center(
          child: StreamBuilder(
            stream: _stream,
            // ??
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.data == "waiting") {
                return Center(child: Text("Waiting of the Quotes....."));
              }
                  // final document = snapshot.data.documents;


                    return GestureDetector(
                    
                      onDoubleTap:(){
                        getQuotes();
                        // snapshot.data.document.length;
                      },
                      child: Stack(
                        children: [
                          ColorFiltered(
                            colorFilter: ColorFilter.mode(Colors.black.withOpacity(0.5), BlendMode.darken),
                            child: FadeInImage.assetNetwork(
                                placeholder: 'assets/loading.gif',
                                image:  _imageUrl,
                              fit: BoxFit.fitHeight,
                              width: double.maxFinite,
                              height: double.maxFinite,
                             ),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(4.0),
                            child: Center(child: Text(snapshot.data.documents['quote'].toString().toUpperCase(),textAlign: TextAlign.center,
                            style: TextStyle(letterSpacing: 0.8,fontSize: 25.0,color: Colors.white,fontWeight: FontWeight.bold),)),
                          ),
                          Column(
                            mainAxisAlignment: MainAxisAlignment.end,
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              Padding(
                                padding: const EdgeInsets.only(bottom:50.0),
                                child: Center(child: Text("-"+snapshot.data.documents['author'].toString().toUpperCase(),textAlign: TextAlign.right,style: TextStyle(letterSpacing: 0.8,fontSize: 18.0,color: Colors.white,fontWeight: FontWeight.bold),)),
                              ),
                            ],
                          ),
                        ],),
                    );
                  }),

        )
        );
  }
}

My cloud firestore]1

1 Answers

It seems you're missing the kind of snapshot in your builder section. You can try this:

Instead of:

final FirebaseFirestore _firestore= FirebaseFirestore.instance;
late Stream _stream= _firestore.collection("quotes").snapshots(); 

You could declare your Stream as:

final Stream<QuerySnapshot> _quotesStream = FirebaseFirestore.instance.collection('quotes').snapshots();

And your Streambuilder should looks like this:

stream: _quotesStream, 
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot)

with the <QuerySnapshot> as in this example.

Related