问题描述
我正在尝试从Friebase-firestore中获取数据,然后将其转换为模型,我对另一个项目(网络项目)使用了相同的代码和方法,它可以正常工作,但是对于这个项目,它不起作用,我不知道为什么.我升级了flutter版本,运行Flutter doctor,环境设置没有问题.这是代码:
I am trying to fetch data from Friebase-firestore and then convert it to model, I used the same code and approach to another project (web project) ,it works fine , but with this project it doesn't work ,I don't know why. I upgrade flutter version, run Flutter doctor , and nothing wrong with the environment setup.here is the code :
class dbService{
static final CollectionReference myCollection =
FirebaseFirestore.instance.collection('myCollection');
static Future<myModel> getMyInfo() async{
myModel sanaa ;
print('this line to print');
final QuerySnapshot myInfo =await myCollection.limit(1).get();
myInfo.docs.forEach((element) {
print(element.data()['name']);
sanaa= new myModel(
name:element.data()['name'].toString(),
title: element.data()["title"].toString(),
img: element.data()['img'].toString(),
);
print('\n print ------ ');
print(element.data()['name']);
print(' that is');
}
);
return sanaa;
}
}
class dbManager{
Stream<myModel> myInfoStream() async*{
yield await dbService.getMyInfo();
}
}
class _aboutStateState extends State<aboutState> {
Stream stream ;
dbManager mng = new dbManager();
@override
void initState() {
// TODO: implement initState
stream = mng.myInfoStream();
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.pink[300],
child: StreamBuilder<myModel>(
stream: stream,
builder: (context, snapshot){
if(snapshot.hasError){
print(snapshot.error);
return Center(child: Text('Something went wrong , please try again later'),);
}
else if(snapshot.hasData){
return Text("${snapshot.data.name}");
}
else {
return Center(child: CircularProgressIndicator());
}
},
),
我收到此错误
this line to print
[cloud_firestore/unknown] NoSuchMethodError: invalid member on null: 'includeMetadataChanges'
但是当我直接连接到Firestore而不将其转换为模型时,它可以正常工作并在屏幕上打印数据,代码为:
but when I connect directly to the firestore without converting it into model ,It works fine and print the data in the screen , the code :
child:FutureBuilder(
future: me.doc(id).get(),
builder: (context, snapshot){
if(snapshot.hasError){
print(snapshot.error);
return Center(child: Text('Something went wrong , please try again later'),);
}
else if(snapshot.hasData){
// print("${snapshot.data.data()['app_pics'][1]} \n${snapshot.data.data()['img']}");
return Text("${snapshot.data.data()['app_pics'][1]}");
}
else {
return Center(child: CircularProgressIndicator());
}
},
),
我去扑扑医生
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, 1.24.0-10.2.pre, on Microsoft Windows [Version 6.3.9600], locale
en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Chrome - develop for the web
[√] Android Studio (version 4.0)
[√] VS Code (version 1.51.0)
[√] Connected device (3 available)
• No issues found!
请我需要帮助
推荐答案
我找到了答案,这是由于Firebase JS SDK的版本引起的.我将index.html文件中的8.1.1替换为7.22.1
I found the answer, it caused because of the version of Firebase JS SDK .I replace 8.1.1 to 7.22.1 in index.html file
<!-- The core Firebase JS SDK is always required and must be listed first -->
<!-- <script src="https://www.gstatic.com/firebasejs/8.1.1/firebase-app.js"></script>-->
<!-- <!– TODO: Add SDKs for Firebase products that you want to use-->
<!-- https://firebase.google.com/docs/web/setup#available-libraries –>- ->
<!-- <script src="https://www.gstatic.com/firebasejs/8.1.1/firebase-firestore.js">
</script>-->
<!-- <script src="https://www.gstatic.com/firebasejs/8.1.1/firebase-analytics.js"></script>-->
我正在将7.22.1版本用于 Firebase JS SDK
I am using 7.22.1 version for Firebase JS SDK
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-analytics.js"></script>
这篇关于Flutter:[cloud_firestore/unknown] NoSuchMethodError:无效的null成员:"includeMetadataChanges"(Flutter网站)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!