Home > database >  How to assign element from list to a variable?
How to assign element from list to a variable?

Time:10-15

Here is my Dart code. Why doesn't it work?

  var myList = ['zero', 'one', 'two', 'three', 'four', 'five'];
  String number = myList[0];

I want number value to be zero. The error I'm getting is:

The instance member 'myList' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expressiondart(implicit_this_reference_in_initializer)

CodePudding user response:

You should not use an other variable when defining other variable in initializer. try assign value to number in initState like this:

  var myList = ['zero', 'one', 'two', 'three', 'four', 'five'];
  String number= '';

  @override
  void initState() {
    super.initState();
    number = myList[0];
  }
  • Related