Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to assign element from list to a variable?

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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];
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading