Flutter Firestore获取数据接收器:空错误 [英] Flutter firestore getting data receiver: null error
问题描述
我进行了很多搜索,但找不到解决方案.我需要你的帮助.
I searched a lot but couldn't find a solution. I need your help.
在我的数据库模型中,我有一个像这样的方法来获取数据:
In my database model, I have a method like this to get the data:
Future<Map<String,dynamic>> getSystemSettings() async {
var response = await _firestore.collection("system").doc("settings").get();
return response.data();
}
稍后,我想以Map< String,dynamic>的形式使用数据.从类中,我想通过调用以下方法从中获取数据.
Later, I want to use the data in the form of Map <String, dynamic> from the class I want to get the data from by calling the method as follows.
getValues() {
database.getSystemSettings().then((value) {
print("value");
print(value);
});
我希望看到我的地图< String,动态>在第二个代码块中键入数据代替值,但是出现以下错误.
What I expect is to see my Map <String, dynamic> type data in place of value in the second code block, but I get the following error.
方法'sistemAyarlariniAl'在null上被调用.
The method 'sistemAyarlariniAl' was called on null.
接收方:空
尝试调用:sistemAyarlariniAl()
Tried calling: sistemAyarlariniAl()
您能帮我吗?我在哪里做错了?
Can you help me with this? Where am I doing wrong?
推荐答案
尝试一下
Future getPosts() async {
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection("system").getDocuments();
return qn.documents;
}
或者您可以使用Stream Builder访问Firebase中的数据.
Or You can use Stream Builder to access data from firebase.
首先,您需要安装 cloud_firestore 库
dependencies:
cloud_firestore: ^0.14.4
然后导入
import 'package:cloud_firestore/cloud_firestore.dart';
然后需要构建StreamBuilder
then need to build StreamBuilder
StreamBuilder(
stream: Firestore.instance
.collection('system')
.document(users.uid)
.snapshots(),
builder: (context, snapshot){
if (snapshot.hasData) {
return Container())
您可以像这样访问数据库值
You Can access your database values like this
snapshot.data['settings'].toString()
TextField中的示例
Example in TextField
Text(
snapshot.data['settings'].toString(),
style: TextStyle(
fontSize: 20,
decorationThickness:2,
fontWeight:
FontWeight.w500,
color:Colors.black87)
),
这篇关于Flutter Firestore获取数据接收器:空错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!