简体   繁体   English

在 DocumentSnapshot 的 get() 之后,程序没有执行 Flutter 中的下一行

[英]After get() of DocumentSnapshot, program is not executing next line in Flutter

Function1 and Function2 are not executed completely. Function1 和 Function2 未完全执行。

//Function1 //函数1

Future<void> getUserDetails() async {
  
    DocumentSnapshot documentSnapshot = await FirebaseFirestore.instance
        .collection('users')
        .doc('aLhON9H3H1ZpZvUB4ISUrK45Hk93')
        .get();
        if (documentSnapshot.exists) {
            globalImagePath = documentSnapshot['imageURL'];
      }
  }

//Function2 (fetch data from subcollection of "users" collection //Function2(从“users”集合的子集合中获取数据

Future<void> fetchPunchinDetails() async {
  
    try {
      var result = await _users
          .doc(user!.uid)
          .collection('attendance')
          .where('outTime', isEqualTo: null)
          .get();

     String dayStart = result.docs[0]['inTime'];
      if (dayStart.isNotEmpty) {
        dayStarted = true;
      }
    } catch (e) {
      print(e.toString());
    }
  }

// Calling above two methods // 调用上面两个方法

_fetchAndGotoScreens() {

    if (loginSuccess = true) {
      getUserDetails();   //--------calling Function1
      fetchPunchinDetails();  //----------calling Function2

     
        //Go to attendance page-----------
        Navigator.of(context)
            .push(
          MaterialPageRoute(
            builder: (context) =>
                Attendance(imagePath: imagePath, dayStartTime: dayStartTime),
          ),
        )
            .catchError((e) {
          print(e);
        });
      } else {
        // go to home page------------
        Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => MyHome()))
            .catchError((e) {
          print(e);
        });
      }
  
  }

When I step through, in getUserDetails() itself, it returns to the calling method just after executing.get().当我单步执行时,在 getUserDetails() 本身中,它会在执行完.get() 之后返回到调用方法。 It is not even checking if (documentSnapshot.exists) condition.它甚至没有检查 (documentSnapshot.exists) 条件。 The same happens with fetchPunchinDetails() function. fetchPunchinDetails() function 也会发生同样的情况。 Please help..请帮忙..

fetchPunchinDetails and getUserDetails are future method, try using await before theses to complete. fetchPunchinDetailsgetUserDetails是未来的方法,在完成之前尝试使用await

_fetchAndGotoScreens() async{

    if (loginSuccess = true) {
     await getUserDetails();   
     await fetchPunchinDetails();   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Flutter 中将 Firestore DocumentSnapshot 转换为 Map - Convert Firestore DocumentSnapshot to Map in Flutter Flutter 遍历 Stream <list<documentsnapshot<object?> >> </list<documentsnapshot<object?> - Flutter iterate through Stream<List<DocumentSnapshot<Object?>>> Flutter Firebase 在同一小部件中返回 Querysnapshot 和 DocumentSnapshot - Flutter Firebase return a Querysnapshot and DocumentSnapshot in the same widget Flutter 云Firestore StreamBuilder<documentsnapshot> 错误</documentsnapshot> - Flutter cloud firestore StreamBuilder<DocumentSnapshot> error 您如何通过 flutter 中的云功能从 firestore 获取 documentSnapshot? - How do you get a documentSnapshot from firestore over a cloud function in flutter? Firebase 文档快照 map 到 Json Flutter 中的 object - Firebase Documentsnapshot map to Json object in Flutter documentSnapshot.data() 不传递给构建器:Flutter - documentSnapshot.data() do not pass to the builder: Flutter containsValue() 不适用于 Flutter 中 Firebase 的 DocumentSnapshot - containsValue() not working with DocumentSnapshot from Firebase in Flutter Flutter NoSuchMethodError 被抛出构建 FutureBuilder<documentsnapshot></documentsnapshot> - Flutter NoSuchMethodError was thrown building FutureBuilder<DocumentSnapshot> 从 Firebase 集合中按 id 获取 DocumentSnapshot - Get DocumentSnapshot by its id from Firebase collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM