【问题标题】:How can I wait for my future function in map function? (Flutter)如何在地图功能中等待我的未来功能? (扑)
【发布时间】:2021-03-19 17:25:59
【问题描述】:

所以,我正在 Flutter 中构建我的应用程序,不幸的是,我最近遇到了一个错误。所以我想在我的 TestProvider 类中做的是从 firestore 获取数据(getQuestionFromFirebase() 函数正在做什么),然后,我想从 DocumentSnapshot 创建一个地图(questionMapFromFirebase() 函数正在做什么)。并且出现了一个错误,因为我无法在 map 函数中异步,所以我的函数不会等待前一个函数的结果,而是返回 null。有什么解决办法吗? *我试图从 getQuestionFromFirebase() - Future 返回地图,但后来我不能使用它的值,因为我的函数需要纯地图。

 class TestProvider {
  FirebaseFirestore _firestore = FirebaseFirestore.instance;

  Future<DocumentSnapshot> getQuestionFromFirebase(String documentId) async {
    return await _firestore.collection('questions').doc(documentId).get();
  }

  Map questionMapFromFirebase(String documentId) {
    Map questionMapFromFirebase;
    getQuestionFromFirebase(documentId).then((DocumentSnapshot carSnapshot) => {
          questionMapFromFirebase = carSnapshot.data(),
        });
    return questionMapFromFirebase;
  }
}

后来我在那里使用了这个功能: 我稍后在那里使用此功能

List<Question> listOfQuestions() {
    List<int> range = numberInRange(amountOfQuestions);
    List<Question> listOfQuestions;
    for (int i = 1; i <= amountOfQuestions; i++) {
      listOfQuestions.add(Question.fromMap(
          _testProvider.questionMapFromFirebase(range[1].toString())));
    }
    return listOfQuestions;
  }

这会在 Future 发生时产生错误。

 The argument type 'Future<Map<dynamic, dynamic>>' can't be assigned to the parameter type 'Map<String, dynamic>'.

编辑: 所以最近我对我的代码做了一些更改,现在看起来像这样

class TestProvider {
  FirebaseFirestore _firestore = FirebaseFirestore.instance;

  Future<DocumentSnapshot> getQuestionFromFirebase(String documentId) async {
    return await _firestore.collection('questions').doc(documentId).get();
  }

  Future<Map> questionMapFromFirebase(String documentId) async {
    DocumentSnapshot ds = await getQuestionFromFirebase(documentId);
    return ds.data();
  }
}

和存储库

class TestRepository {
  final int amountOfQuestions;
  TestRepository({
    @required this.amountOfQuestions,
  });

  TestProvider _testProvider;

  Future listOfQuestions() async {
   List<int> range = numberInRange(amountOfQuestions);
   List<Question> listOfQuestions;
   for (int i = 1; i <= amountOfQuestions; i++) {
    listOfQuestions.add(Question.fromMap(
      await _testProvider.questionMapFromFirebase(range[i].toString())));
   }
  return listOfQuestions;
  }
}

我开始看到的问题是,每次我尝试从 TestProvider 调用函数 questionMapFromFirebase 时,它​​都工作得很好。但是当我尝试从 TestRepository 调用它时,它会抛出错误:

E/flutter (13348): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method 'questionMapFromFirebase' was called on null.
E/flutter (13348): Receiver: null
E/flutter (13348): Tried calling: questionMapFromFirebase("2")

还有其他建议我该如何处理?

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:
    Future<Map> questionMapFromFirebase(String documentId) async {
       DocumentSnapshot ds = await getQuestionFromFirebase(documentId);
       return ds.data();
      }
    

    编辑 检查FutureBuilder

    例如,它将位于需要显示列表的小部件树中。

    return FutureBuilder(
      future: _loadQuestions(),
      builder: (context, snapshot) {
        if(snapshot.connectionState == ConnectionState.done){
          return widgetForListing(snapshot.data);
        }
        return Center(child: Text('Loading...'));
      },
    );
    

    您的_loadQuestions 函数将是

    _loadQuestions() async {
        List<int> range = numberInRange(amountOfQuestions);
        List<Question> listOfQuestions = [];
        for (int i = 1; i <= amountOfQuestions; i++) {
            listOfQuestions.add(Question.fromMap(
          await _testProvider.questionMapFromFirebase(range[1].toString())));
        }
        return listOfQuestions; //you can get this list in **snapshot.data** of future builder
    }
    

    【讨论】:

    • 就像我在 * 中所说的,它必须是一张纯地图,因为后来我得到:The argument type 'Future&lt;Map&lt;dynamic, dynamic&gt;&gt; can't be assigned to the parameter type 'Map&lt;String, dynamic&gt;'. 错误
    • 你会在哪里使用这张纯地图?有很多方法可以获得实际结果,而不是包裹在未来中。
    • 我扩展了我的问题。
    • listOfQuestions 会显示?
    • 是的,此列表将被使用
    猜你喜欢
    • 2020-07-01
    • 2020-10-31
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    相关资源
    最近更新 更多