3

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]?

3
  • Please post a Minimal, Complete, and Verifiable example of your attempt, noting input and expected output. Commented Oct 9, 2018 at 12:12
  • When I store the value in the list at 0 position, I want it to be stored actually in the variable _uTypeSelected and it should not edit the actual list (The list is only to contain those variables). Commented Oct 9, 2018 at 12:19
  • Strings are immutable, so you can't change the state. Commented Oct 9, 2018 at 12:33

1 Answer 1

1

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";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.