Home > Blockchain >  Re-instantiate Class variable in Dart / Flutter
Re-instantiate Class variable in Dart / Flutter

Time:09-21

At the beginning of my app I have a global variable gameData which is declared and instantiated as:

GameData gameData = GameData();

Later I want to clear the gameData variable and re-instantiate/reset the variable with a clean instance of GameData. I do this by calling a function:

void ResetGameData() {
  gameData = new GameData();
} 

But that's not clearing the gameData variable. All the old values are remaining. Is there a better way of doing this?

CodePudding user response:

It seems like your approach should work.

If you're building your UI based off of GameData, you will need to call setState() or notifyListeners() to rebuild everything.

CodePudding user response:

The issue was I was instantiating a class within a class, and that syntax was incorrect so the sub-class was retaining its previous data. The rest of the variable was re-instantiated correctly.

  • Related