Flutter firestore getting data receiver: null error

半腔热情 提交于 2021-01-29 19:56:30

问题


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();
  }

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

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.

The method 'sistemAyarlariniAl' was called on null.

Receiver: null

Tried calling: sistemAyarlariniAl()

Can you help me with this? Where am I doing wrong?


回答1:


Try This

  Future getPosts() async {
        var firestore = Firestore.instance;
        QuerySnapshot qn = await firestore.collection("system").getDocuments();
        return qn.documents;
      }

Or You can use Stream Builder to access data from firebase.

to do that first you need to install cloud_firestore library

    dependencies:
          cloud_firestore: ^0.14.4

then import it

    import 'package:cloud_firestore/cloud_firestore.dart';

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()

Example in TextField

Text(
       snapshot.data['settings'].toString(),
       style: TextStyle(
              fontSize: 20,
              decorationThickness:2,
              fontWeight:
              FontWeight.w500,
              color:Colors.black87)
    ),


来源:https://stackoverflow.com/questions/65405430/flutter-firestore-getting-data-receiver-null-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!