Home > OS >  Is it possible to store a variable in a list in flutter?
Is it possible to store a variable in a list in flutter?

Time:11-06

I want to put the following variables in a list by grouping them into a class or making them a global variable... How can I put it on the list? Is it possible to put these variables in a list?

The button below is a favorite variable that is saved locally. It changes frequently.

ex.favorite[index]

bool favoriteButton_0_01_01 = false;
bool favoriteButton_0_01_02 = false;
bool favoriteButton_0_01_03 = false;
bool favoriteButton_0_01_04 = false;
bool favoriteButton_0_01_05 = false;
bool favoriteButton_0_01_06 = false;
bool favoriteButton_0_01_07 = false;
bool favoriteButton_0_01_08 = false;
bool favoriteButton_0_01_09 = false;
bool favoriteButton_0_01_10 = false;
bool favoriteButton_0_01_11 = false;
bool favoriteButton_0_01_12 = false;
bool favoriteButton_0_01_13 = false;
bool favoriteButton_0_01_14 = false;
bool favoriteButton_0_01_15 = false;
bool favoriteButton_0_01_16 = false;
bool favoriteButton_0_01_17 = false;
bool favoriteButton_0_01_18 = false;
bool favoriteButton_0_01_19 = false;
bool favoriteButton_0_01_20 = false;
bool favoriteButton_0_01_21 = false;
bool favoriteButton_0_01_22 = false;
bool favoriteButton_0_01_23 = false;
bool favoriteButton_0_01_24 = false;
bool favoriteButton_0_01_25 = false;
bool favoriteButton_0_01_26 = false;
bool favoriteButton_0_01_27 = false;
bool favoriteButton_0_01_28 = false;
bool favoriteButton_0_01_29 = false;
bool favoriteButton_0_01_30 = false;

CodePudding user response:

Use a List

List<bool> favoriteButtons = [
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
];

Or better yet:

List<bool> favoriteButtons = List.filled(30, false);

Use it like:

var buttonValue = favoriteButtons[3]; // Or whatever index

CodePudding user response:

You should use Map as suggested by שו אוהב אותך and then use favorites.values.toList()[index]

Map<String, bool> favorites = {
            'favoriteButton_0_01_01': false,
            'favoriteButton_0_01_02': false
          };
          print(favorites.values.toList()[0]);
  • Related