尝试从我们的Firestore数据库获取数据字段,但整数返回为null

时间:2019-06-26 20:33:24

标签: asynchronous flutter dart google-cloud-firestore widget

我代码中的方法应该可以捕获我们的调查移动应用程序提出问题所需的超时。但是,即使我们建立了应该保存数据副本的文档快照,该方法也只会返回null。我们有一个后备硬编码,因此当超时为null时,它将返回一个默认小部件。

我们已经尝试了小部件中的几种异步并等待其自身的方法,但它们似乎都无法使小部件等待来自Firestore文档的超时。

const fiveSeconds = Duration(seconds: 5);
Future<int> getTimeOutData() async{
  int toReturn;
  Firestore.instance.collection("config").getDocuments().then((DocumentSnapshot) async=>{
    Future.delayed(fiveSeconds, () async => toReturn = await DocumentSnapshot.documents[0]['timeout']),
    print( toReturn)
  });
  return toReturn;
}
Widget _buildListItem(BuildContext context, DocumentSnapshot doc) {
  return ListTile(
    title: Text(
      doc['question_text'].toString(),
      style: Theme.of(context).textTheme.headline,
    ),
    dense: true,
    trailing: Icon(Icons.keyboard_arrow_right),
    contentPadding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
    onTap: () async{
      timeout= await getTimeOutData();
      envelope = new Envelope(doc['complete'], doc.documentID, doc['user'],
          doc['question'], doc['answer_text'], doc['answer_type'], doc['time_stamp']);
       Navigator.push(
        context,
         MaterialPageRoute(
          builder: (context) {
            return ViewAnswerController(envelope,  timeout);
          },
        ),
      );
    },
    selected: true,
  );
}

我期望1毫秒,但是在方法中打印时以及随后在其他小部件中检查时,实际值为null。

1 个答案:

答案 0 :(得分:0)

const fiveSeconds = Duration(seconds: 5);
Future<int> getTimeOutData() async{
  int toReturn;
  await Firestore.instance.collection("config").getDocuments().then((DocumentSnapshot) async=>{
    await Future.delayed(fiveSeconds, () async => toReturn = await DocumentSnapshot.documents[0]['timeout']),
    print( toReturn)
  });
  return toReturn;
}

没关系,因为.then()也是一个Future对象,所以我只需要等待更多时间。