Issue type List<Object> is not a subtype of type List<Map<String, Object>> in type cast

Hey Guys,

Im pretty new in Dart/Flutter and Im currently developing an simple Quiz-App to get a fundamental understanding how Flutter/Dart works. But Ive got an issue which I cant fix for hours and almost days.

If I wanna start my Android Emulator it says :

List is not a subtype of type List<Map<String, Object>> in type cast

And that’s probably the code:

@override

Widget build(BuildContext context) {

return Column(

  children: <Widget>[

    Question(

      questions[questionIndex]['questionText'],

    ),

    ...(questions[questionIndex]['answers'] as List<Map<String, Object>>).map((answer) {

      return Answer(() => answerQuestion(answer['score']), answer['text']);

    }).toList()

  ],

);

I look forward to the answeres. :slight_smile:

Hi @Trite, if you’re still having trouble, could you send all the files that include this questions variable?

3 Likes

Thank you for your help… Ive got fixed the problem… There was an issue in my map…

3 Likes

Hello, do you mind sharing what the exact problem was and how you fixed it?

Can you please help with the solution, Even i have the same issue @Trite

Heyy @lohiths42 Could you share a part of your code that’s causing the problem? This seems to be a problem related assignment between invalid types and can be easily solved.

If you dont want to do strict type checking then change List<Map<String, Object>> to List. Else check the value that you are receiving from the API, it may be of some format other than List<Map<String, Object>> .

Hello @sakina, I’m new to Flutter,
Thanks for the reply
Even I was trying to work on basic quiz app, and this is the error which I’m stuck at

return Column(
children: [
Question(
questions[questionIdx][‘questionText’],
),
…(questions[questionIdx][‘answerText’] as List<Map<String, Object>>>).map((answer) {
return Answer(() => answerQuestion(answer[‘score’]), answer[‘text’]);
}).toList()
],
);

and this is the list which i created

final _questions = const [
{
‘questionText’: ‘What is your name?’,
‘answerText’: [
{‘text’: ‘Lohith’, ‘score’: 100},
{‘text’: ‘Ranjt’, ‘score’: 40},
{‘text’: ‘sads’, ‘score’: 50},
{‘text’: ‘poyt’, ‘score’: 45}
],
},
{
‘questionText’: ‘What is your animal?’,
‘answerText’: [
{‘text’: ‘dog’, ‘score’: 32},
{‘text’: ‘cat’, ‘score’: 15},
{‘text’: ‘parrot’, ‘score’: 10}
],
},
{
‘questionText’: ‘What is your Fav food?’,
‘answerText’: [
{‘text’: ‘chicken’, ‘score’: 32},
{‘text’: ‘mutton’, ‘score’: 32},
{‘text’: ‘fish’, ‘score’: 32},
{‘text’: ‘prawn’, ‘score’: 32},
],
}
];

Can some one please reply on this, Im stuck on this code, for 3 days

Heyy! @lohiths42 So sorry for the late reply. Try changing the below code

as List<Map<String, Object>>>
to
as List<Map<String, dynamic>>>
or
as List<>

In this list, the second parameter is not an object but it’s actually a text or number. Do ping here if you are still facing the issue.

‘answerText’: [
{‘text’: ‘chicken’, ‘score’: 32},
{‘text’: ‘mutton’, ‘score’: 32},
{‘text’: ‘fish’, ‘score’: 32},
{‘text’: ‘prawn’, ‘score’: 32},
],

@sakina Thanks a lot, I resolved my error, by changing it to dynamic.

1 Like