gpt4 book ai didi

firebase - 我怎样才能在 map 功能中等待我 future 的功能? ( flutter )

转载 作者:行者123 更新时间:2023-12-04 15:11:01 25 4
gpt4 key购买 nike

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

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

还有其他问题我该如何处理?

最佳答案

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
}

关于firebase - 我怎样才能在 map 功能中等待我 future 的功能? ( flutter ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65198108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com