Sunisha Guptan
Sunisha Guptan

Reputation: 1605

Why variable value is not refreshing in flutter?

The variable value is not refreshing in flutter(which I created outside the class). The following way I have added.So in button press am setting different value. So if I come back to that screen again it is not showing 1. May I know why it is happening?

int _currVal = 1;

class AskFragment extends StatefulWidget {
  static const String routeName = "//";
  static const
  int currState = -1;

  @override
  HomeScreenState createState() => HomeScreenState();
}

Upvotes: 2

Views: 5128

Answers (1)

Yogesh Chawla
Yogesh Chawla

Reputation: 723

To update the values, use Stateful Widget and initialise values inside that Widget only, for any updates in the values of variables mention that in setState((){}) method to notify the change in the values.

See this for documentation of stateful widgets

See this for documentation of setState((){}) method

setState(() { _myVariable = newValue });

Note:- Never initialise changing values in build method they will not get updated instead they will be reinitialised with same values again and again

Upvotes: 5

Related Questions