Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

flutter - Unhandled Exception: setState() called in constructor: ReservationMakingState#861c9(lifecycle state: created, no widget, not mounted)

Im creating an app thats making reservations for boat rentals, but i struggle to fix this problem any1 can help? Providing code samples:


          if (docs.size == 0) {
            reservations.doc(resID).set({
              'resID': resID,
              'name': name,
              "surname": surname,
              "phoneNumber": phoneNumber,
              "rental": ProductCardInCart.rental,
              'boat': ProductCardInCart.boat,
              'date': date,
              'time': time
            }).then((value) {
              print("Reservation made");
              setState(() {
                Body.empty = true;
              });
            }).catchError((error) => print("Failed to reservate: $error"));
          } else if (docs.size != 0) {
            setState(() {
              Body.empty = false;
            });
            print('jestjus');
          }
        });
      } else {
        return;
      }
    });
  }
}

this setstate is working well

.then((value) {
              print("Reservation made");
              setState(() {
                Body.empty = true;
              });

but this one isnt and its throwing exception

} else if (docs.size != 0) {
            setState(() {
              Body.empty = false;
            });
            print('jestjus');
          }

Error that its throwing:

7980): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: setState() called in constructor: ReservationMakingState#861c9(lifecycle state: created, no widget, not mounted)
E/flutter ( 7980): This happens when you call setState() on a State object for a widget that hasn't been inserted into the widget tree yet. It is not necessary to call setState() in the constructor, since the state is already assumed to be dirty when it is initially created.

Thanks for help in advance


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The reason that your setState isn't working is because your widget is still being built. It can't update until AFTER the widget has completed its first build.

the reason that this one works

.then((value) {
   print("Reservation made");
   setState(() {
     Body.empty = true;
   });
});

is because it is happening after a future .then(() {});

if you would like to have your widget rebuild after it has initially been set up you can use
Be careful using this. if you use it within your build method, you will get stuck in a loop! You should have this in a initState method or have some conditional statement before this is run

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      //do something call your set state here
    });
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...