Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase Firestore、Flatter、Async:方法未等待异步方法完成_Firebase_Flutter_Asynchronous_Google Cloud Firestore_Async Await - Fatal编程技术网

Firebase Firestore、Flatter、Async:方法未等待异步方法完成

Firebase Firestore、Flatter、Async:方法未等待异步方法完成,firebase,flutter,asynchronous,google-cloud-firestore,async-await,Firebase,Flutter,Asynchronous,Google Cloud Firestore,Async Await,我正在构建一个Flitter应用程序,它使用API获取加密货币的价格。我将API密钥存储在Firestore数据库中,目前我可以从Firestore检索API密钥,以便在我的应用程序中使用。我遇到的问题是,当运行buildURL()时,它不会等待String apiKey=wait getApiKey()在继续之前完全完成,导致apiKey从buildURL()打印为空 我在getApiKey()和buildURL()中添加了print语句来跟踪apiKey的值,并且似乎buildURL()中的

我正在构建一个Flitter应用程序,它使用API获取加密货币的价格。我将API密钥存储在Firestore数据库中,目前我可以从Firestore检索API密钥,以便在我的应用程序中使用。我遇到的问题是,当运行
buildURL()
时,它不会等待
String apiKey=wait getApiKey()
在继续之前完全完成,导致
apiKey
buildURL()
打印为空

我在
getApiKey()
buildURL()
中添加了print语句来跟踪
apiKey
的值,并且似乎
buildURL()
中的print语句在
getApiKey()
中的print语句之前运行



为了等待函数,它需要是一个异步函数。 等待函数需要将
async
await
添加到
getApiKey()

 Future<String> getApiKey() async {
    var result = await FirebaseFirestore.instance
        .collection("myCollection")
        .doc("myDocument")
        .get();
    return result.data()["Key"]
  }
Future getApiKey()异步{
var result=等待FirebaseFirestore.instance
.收集(“myCollection”)
.doc(“myDocument”)
.get();
返回result.data()[“Key”]
}

为了等待函数,它需要是一个异步函数。 等待函数需要将
async
await
添加到
getApiKey()

 Future<String> getApiKey() async {
    var result = await FirebaseFirestore.instance
        .collection("myCollection")
        .doc("myDocument")
        .get();
    return result.data()["Key"]
  }
Future getApiKey()异步{
var result=等待FirebaseFirestore.instance
.收集(“myCollection”)
.doc(“myDocument”)
.get();
返回result.data()[“Key”]
}
您介意试一试吗

  Future<String> getApiKey() async {
  String result=await  FirebaseFirestore.instance
        .collection("myCollection")
        .doc("myDocument")
        .get()
        .then((value) {
      print("Api Key from getApiKey():");
      print(value.data()["Key"]);
      return value.data()["Key"];
    });
   return result;
  }
Future getApiKey()异步{
字符串结果=等待FirebaseFirestore.instance
.收集(“myCollection”)
.doc(“myDocument”)
.get()
.然后((值){
打印(“来自getApiKey()的Api密钥:”;
打印(value.data()[“Key”]);
返回值.data()[“Key”];
});
返回结果;
}
您介意试一试吗

  Future<String> getApiKey() async {
  String result=await  FirebaseFirestore.instance
        .collection("myCollection")
        .doc("myDocument")
        .get()
        .then((value) {
      print("Api Key from getApiKey():");
      print(value.data()["Key"]);
      return value.data()["Key"];
    });
   return result;
  }
Future getApiKey()异步{
字符串结果=等待FirebaseFirestore.instance
.收集(“myCollection”)
.doc(“myDocument”)
.get()
.然后((值){
打印(“来自getApiKey()的Api密钥:”;
打印(value.data()[“Key”]);
返回值.data()[“Key”];
});
返回结果;
}

您没有从getApiKey函数返回

getApiKey() {
    return FirebaseFirestore.instance
        .collection("myCollection")
        .doc("myDocument")
        .get()
        .then((value) {
      print("Api Key from getApiKey():");
      print(value.data()["Key"]);
      return value.data()["Key"];
    });
  }

您没有从getApiKey函数返回

getApiKey() {
    return FirebaseFirestore.instance
        .collection("myCollection")
        .doc("myDocument")
        .get()
        .then((value) {
      print("Api Key from getApiKey():");
      print(value.data()["Key"]);
      return value.data()["Key"];
    });
  }