Search code examples
flutterlistdartobjectmapping

the argument type object cann't be assigned to parameter like string in flutter while accessing the map inside the list


//my code:
var _index = 0;
  var question = [
    {
      'Questionkey': "which cryptocurrency do you like",
      'Answer': ['bitcoin', 'etherium']
    },
    {
      'Questionkey': "and why do you like that?",
      'Answer': ['stable', 'acutally i don\'t know ']
    },
  ];



question[_index]['Questionkey']//accessing the question but I am getting error while accessing

//question is the list that consists of maps and I want to access the question which is pointed questionkey key


Solution

  • Wherever you use

    question[_index]['Questionkey']
    

    write

    question[_index]['Questionkey'] as String
    

    instead. It's able to access the map just fine, it's just that it can't determine the type at compile time. And I assume you are using it at a spot where it requires to be a String. Knowing that it is a String you can safely cast it like this