Vishnu
Vishnu

Reputation: 1997

OnStateChanged Method in Flutter

In Flutter, is there a method like onStateChanged which is called when the state of the page changes?

setState(() {
  widget._loadCompleted = true;
  widget._loading = false;
});

I'm trying to set the two bool values in setState() method. I'm setting states for several other reasons. So I want to know if the last state change was for this particular reason.

Upvotes: 1

Views: 1742

Answers (1)

CopsOnRoad
CopsOnRoad

Reputation: 268264

As Günter mentioned, there is no such thing like onStateChanged(). You have to deal it in build() method.

If I got you right, you can use like this:

class _MyAppState extends State<MyApp> {
  bool myFlag = false; // initially set to false

  void _doYourWork() {
    setState(() => myFlag = true); // set to true here
  }

  @override
  Widget build(BuildContext context) {
    if (myFlag) {
      // setState() just got called
    } else {
       // we are building fresh for the first time. 
    }
    myFlag = false;
    return yourWidget();
  }
}

After this build() will receive myFlag value to true and it can then set to false again. So, you can do this trick.

Upvotes: 3

Related Questions