class' _JSONQORYSNAPSHOT'没有实例getter'文档'
请我尝试使用图像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),)),
),
],
),
],),
);
}),
)
);
}
}
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),)),
),
],
),
],),
);
}),
)
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您缺少建造者部分中的快照。您可以尝试以下操作:
而不是:
您可以将流将其声明为:
和您的streambuilder应该看起来像:
使用
&lt; querySnapShot&gt;
如It seems you're missing the kind of snapshot in your builder section. You can try this:
Instead of:
You could declare your Stream as:
And your Streambuilder should looks like this:
with the
<QuerySnapshot>
as in this example.