0

Have a look at my code here it's in dart programing language I have a map object which I'm trying to assign inside another map object('question'). Coming from a Python background this usually works since I'm new to dart I need help.

Map ageData = {
    "Below 18": 'hello kid',
    "Above 18": 'hello Young adult',
    'Between 28-58': 'hello adult',
    "Greater Than 58": "hello senior citizen"
};

var question = [{"questionText":"Age Group", "opt":[ageData]}]; 
// I get error here stating "The instance member 'ageData' can't be 
// accessed in an initializer. Try replacing the reference to the 
// instance member with a different expression" 
fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
Harshith Yadav
  • 106
  • 1
  • 9
  • can you confirm the error is with this code-block? I copied this code-block to dart pad and didn't get an error, I printed the object and it worked fine – Rohan Thacker Apr 24 '21 at 13:40
  • i tried that too. it works but my vs code extension is throwing this error – Harshith Yadav Apr 24 '21 at 13:46
  • Is this the full code? is it possible that the error occurs in another place in the code? As we know this code works fine – Rohan Thacker Apr 24 '21 at 13:47
  • I'm coding for flutter framework using the vscode and this is the only place where I'm getting the error – Harshith Yadav Apr 24 '21 at 13:52
  • when the piece of code is in void main(), then there is no error. but put it inside a class like so ```class NewAppState { final final_data = []; var i = ''; Map ageData = { "Below 18": 'hello kid', "Above 18": 'hello Young adult', 'Between 28-58': 'hello adult', "Greater Than 58": "hello senior citizen" }; Map genderData = {"Male": "Male", "Female": "Female"}; Map educationData = { "School": "Student Student", "College": "College Student", "Other": "Other" }; List question = [{"questionText":"Age Group","opt":ageData}];}``` – Harshith Yadav Apr 24 '21 at 13:58
  • See https://stackoverflow.com/a/64548861/. – jamesdlin Apr 24 '21 at 20:15

2 Answers2

1

As this code is within a class, you will not be able to access instance members in the initializer

However you can access this property if it is set to static as static members are available on all instances of the class.

class NewAppState {
  NewAppState();
  final final_data = [];
  var i = '';

  static Map ageData = {
    'Below 18': 'hello kid',
    'Above 18': 'hello Young adult',
    'Between 28-58': 'hello adult',
    'Greater Than 58': 'hello senior citizen'
  };

  Map genderData = {'Male': 'Male', 'Female': 'Female'};

  Map educationData = {
    'School': 'Student Student',
    'College': 'College Student',
    'Other': 'Other'
  };

  List question = [
    {'questionText': 'Age Group', 'opt': ageData}
  ];
}

Rohan Thacker
  • 5,377
  • 3
  • 21
  • 34
-1

I did some reading on the documentation it says that I would have to make it static so i tried this

class NewAppState  {
  final final_data = [];
  var i = '';
  static Map ageData = {
    "Below 18": 'hello kid',
    "Above 18": 'hello Young adult',
    'Between 28-58': 'hello adult',
    "Greater Than 58": "hello senior citizen"
  };
  Map genderData = {"Male": "Male", "Female": "Female"};
  Map educationData = {
    "School": "Student Student",
    "College": "College Student",
    "Other": "Other"
  };
 
  List question = [{"questionText":"Age Group","opt":ageData}];}

and now it works fine I just added that one word static and it got fixed :) anyone curious can read the docs here open this link for more info

Harshith Yadav
  • 106
  • 1
  • 9