Keshav
Keshav

Reputation: 1091

How to make a list of variables in flutter?

I want to create a list in flutter that contains variable. Now when I am trying to edit the contents of the list, it actually gets stored in the list itself. For eg.,

static var _ratingSelected = "";
static var _disconnectSelected = "";
static var _uTypeSelected = "";

List finalSelectedList = [_uTypeSelected,_disconnectSelected,_ratingSelected];

This is my list, when I will edit the list, like,

finalSelectedList[0] = "Main";

The output list will be like

finalSelectedList = ["Main",_disconnectSelected,_ratingSelected];

It will edit the finalSelectedList[0] position of the list but not the variable _uTypeSelected. Is there any way so that the value gets stored in the variable _uTypeSelected and not at the position finalSelectedList[0]?

Upvotes: 3

Views: 11215

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657058

There is no way to do that. If you use finalSelectedList[0].someProp = "Main"; where the variables are instances of some classes that have a someProp property, then it would work, because there is an additional abstraction. Strings are primitive values and copied by value when added to a list and therefore there is no connection left between list entry and variable.

class Foo {
  Foo(this.someProp);
  String someProp;
}
static var _ratingSelected = Foo("");
static var _disconnectSelected = Foo("");
static var _uTypeSelected = Foo("");

List finalSelectedList = [_uTypeSelected,_disconnectSelected,_ratingSelected];
finalSelectedList[0].someProp = "Main";

Upvotes: 1

Related Questions