class' _JSONQORYSNAPSHOT'没有实例getter'文档'

发布于 2025-02-07 12:58:47 字数 4772 浏览 1 评论 0原文

请我尝试使用图像URL制作报价应用程序,并引用Cloud Firestore获得的引用。我已经收集了一个“引号”的集合,其中包含“报价”字段和“作者”的文档,现在我正在尝试从Firestore访问该集合,但我一直在遇到这个问题。我的计划是,当用户双击屏幕时,它将获得新的随机图像以及“引号”集合中的下一个文档。这是我下面的代码。我的云燃烧片显示在下图中。

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),)),
                              ),
                            ],
                          ),
                        ],),
                    );
                  }),

        )
        );
  }
}

我的云firestore ] 1

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哑剧 2025-02-14 12:58:47

看来您缺少建造者部分中的快照。您可以尝试以下操作:

而不是:

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

您可以将流将其声明为:

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

和您的streambuilder应该看起来像:

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

使用&lt; querySnapShot&gt;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文