Firebase Flutter .once()。then()在重新运行时永远不会更新

时间:2019-04-25 20:06:03

标签: firebase firebase-realtime-database dart flutter

我一直在使用database.re .... once()。then(

当此代码首次运行时,它会获取信息,然后在再次运行时再也不会更改。

嵌套它们会引起问题,我正在嵌套它们以避免类型,因为在尝试对某事调用if语句时无法摆脱它。

database Image

if (_scaffoldType == ScaffoldType.lecturer) {
        .....

      } else {
        print(
            "this time $oneTime - this time ${tempCode[1]} it should equal less than 6");
        var clasDN;
        if (oneTime - int.parse(tempCode[1]) <= 6) {
          print("---------------------time is good ");
          var ata;
          database
              .reference()
              .child('tempCode')
              .child(tempCode[0])
              .once()
              .then((DataSnapshot snapshot) {
            if (snapshot.value == null) {
              ata = false;
            } else {
              ata = true;
            }
            if (ata == true) {
              print("-----------------------temp is good");
              database
                  .reference()
                  .child('tempCode')
                  .child(tempCode[0])
                  .once()
                  .then((DataSnapshot snapshot) {
                Map<dynamic, dynamic> data = snapshot.value;
                var clasD = data['classId'];
                print("classId is -------$clasD-------");
                database
                    .reference()
                    .child('classData')
                    .child(clasD)
                    .once()
                    .then((DataSnapshot snapshot) {
                  Map<dynamic, dynamic> data = snapshot.value;
                  clasDN = data['classDaysPast'].toString();
                  print("classDaysPast is -------$clasDN-------");
                  database
                      .reference()
                      .child('user')
                      .child(userUid)
                      .once()
                      .then((DataSnapshot snapshot) {
                    Map<dynamic, dynamic> data = snapshot.value;
                    var uniId = data['uniId'];
                    var ate;
//                  atenUs()async {
                    database
                        .reference()
                        .child('classData')
                        .child(clasD)
                        .child('attendance')
                        .child(clasDN)
                        .child(userUid)
                        .once()
                        .then((DataSnapshot snapshot) {
                      print("snaaaaaaaaaap -----${snapshot.value}");
                      if (snapshot.value == null) {
                        setState(() {
                          ate = false;
                        });
                      } else {
                        setState(() {
                          ate = true;
                        });
                      }
                      print("=-=-=-=-=-=$ate");
//                      ate;
                      print("ate is ate is $ate");
                      print("-----------------");
                      if (ate == false) {
                        print("-------------database attendacne is null");
                        database
                            .reference()
                            .child('classData')
                            .child(clasD)
                            .child('attendance')
                            .child(clasDN)
                            .child(userUid)
                            .set({
                          'userId': userUid,
                          'uniId': uniId,
                        });
                        var tta;
//                        cad() {
                        database
                            .reference()
                            .child('classData')
                            .child(clasD)
                            .child('userAttendace')
                            .child(userUid)
                            .once()
                            .then((DataSnapshot snapshot) {
                          if (snapshot.value == null) {
                            tta = false;
                          } else {
                            tta = true;
                          }
//                        tta = snapshot.value;
                          print("-------tta-------$tta");
//                            return tta;

                          print("-----------cad------$tta");
                          if (tta() == false) {
                            print("didn't reach here");
                            database
                                .reference()
                                .child('classData')
                                .child(clasD)
                                .child('userAttendace')
                                .child(userUid)
                                .set({
                              "userId": userUid,
                              "uniId": uniId,
                              'dayHere': -1,
                            });
                          } else {
                            print("reached here");
                            database
                                .reference()
                                .child('classData')
                                .child(clasD)
                                .child('userAttendace')
                                .child(userUid)
                                .once()
                                .then((DataSnapshot snapshot) {
                              Map<dynamic, dynamic> data = snapshot.value;
                              var dayHere = data['dayHere'];
                              print("------------ day here $dayHere");
                              dayHere = dayHere + 1;
                              print("------------ day here $dayHere after");
                              database
                                  .reference()
                                  .child('classData')
                                  .child(clasD)
                                  .child('userAttendace')
                                  .child(userUid)
                                  .update({
                                'dayHere': dayHere,
                              });
                            });
                          }
                        });
//                        }
                      }
                      //Warning are already attended! TODO
                      else {
                        Flushbar(title: "Error",message: "Already attended",duration: Duration(seconds: 5),isDismissible: false,).show(context);
                        print("-------------database attendacne is  NOT null");
                      }
                    });
//                  }
//                  atenUs();
                  });
                });
              });
            }
            //wrong tempCode TODO
            else {
              Flushbar(title: "Error",message: "Wrong CODE try again!",duration: Duration(seconds: 5),isDismissible: false,).show(context);
              scan();
            }
          });
        }
        //wrong time TODO
        else {
          Flushbar(title: "Error",message: "Wrong TIME try again!",duration: Duration(seconds: 5),isDismissible: false,).show(context);
          scan();
        }

else节可能没有运行。

日志

I/flutter (11231): ----------------[m1p2EsQd, 1556222330]
I/flutter (11231): this time 1556222331 - this time 1556222330 it should equal less than 6
I/flutter (11231): ---------------------time is good 
I/flutter (11231): -----------------------temp is good
I/flutter (11231): X3cphL72DHMTOOZANaxpAUS35ix1
I/flutter (11231): classId is -------IV3b9CN9nssAll8mcp-------
I/flutter (11231): false
I/flutter (11231): (-1, false, 123456789)
I/flutter (11231): student
I/flutter (11231): Start
I/flutter (11231): classDaysPast is -------37-------
I/flutter (11231): snaaaaaaaaaap -----{userId: X3cphL72DHMTOOZANaxpAUS35ix1, uniId: 123456789}
I/flutter (11231): =-=-=-=-=-=true
I/flutter (11231): ate is ate is true
I/flutter (11231): -----------------
I/flutter (11231): -------------database attendacne is  NOT null
I/flutter (11231): X3cphL72DHMTOOZANaxpAUS35ix1
I/flutter (11231): Start
I/flutter (11231): false
I/flutter (11231): (-1, false, 123456789)
I/flutter (11231): student

0 个答案:

没有答案