Search code examples
androidflutterdartobjectlayout

Error: The method 'map' isn't defined for the class 'Object?'


var questions = [
  {
    'questionText' : 'What\'s your favorite colour? ',
    'answers' : [ 'Green', 'Red', 'Yellow', 'Black'],
  },
];
return MaterialApp(
  home: Scaffold(
    appBar: AppBar(
      title: Text('My second app'),
    ),
    body: Column(
      children: [
        Question(
            questions[_questionIndex]['questionText'] as String
        ),
        **questions[_questionIndex]['answers'].map((answer){
          return Answer('', answer);
          }),**

I tried to put "as String" but nothing worked, the compiler always returns the same error


Solution

  • You need to let dart know what questions[_questionIndex]['answers'] is, you can use as to do this:

    (questions[_questionIndex]['answers'] as List<String>).map((answer){