类"_JsonQuerySnapshot"没有实例获取器"文档"



请我试图使一个引用应用程序使用图像Url和从云firestore获得的报价。我已经收集了一些"引言"。使用带有"quote"字段的文档和";author"现在我试图从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),)),
),
],
),
],),
);
}),
)
);
}
}

My cloud firestore]1

似乎你错过了那种快照在你的构建器部分。你可以试试:

代替:

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)

与本例中的<QuerySnapshot>

最新更新