Abdessamad Jadid
Abdessamad Jadid

Reputation: 401

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

I was watching a tutorial about Learning Flutter and Dart using VSCode as IDE. while running my app i got an Cast Error : Type 'List<Map<Object, Object>>' is not a subtype of type 'List<Map<String, Object>>' in type cast

My application is a quiz app

Here is the Widget in quizz.dart file:

Widget build(BuildContext context) {
    return Column(
      children: [
        Question(questions[questionIndex]['questionText'],),

        //mapping the answers from
         ...(questions[questionIndex]['answers'] as List<Map<String, Object>>)
            .map((answer) {
           return Answer(() => answerQuestion('score'), answer['Text']);
         }).toList()
      ],
    );
}

Here is the main.dart file:

import 'package:flutter/material.dart';
import './quiz.dart';
import './result.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
// createState method for returning the State Class inherited by StatefulWidget
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

// MyAppState Class inherited from State Class Based on StatefulWidget
class _MyAppState extends State<MyApp> {
  //initialized variable

  final _questions = const [
    {
      'questionText': 'What\'s your favorite color?',
      'answers': [
        {Text: 'Black', 'score': 1},
        {Text: 'Red', 'score': 3},
        {Text: 'Black', 'score': 5},
        {Text: 'White', 'score': 8}
      ]
    },
    {
      'questionText': 'What\'s your favorite animal?',
      'answers': [
        {Text: 'Rabbit', 'score': 15},
        {Text: 'Snake', 'score': 10},
        {Text: 'Elephant', 'score': 5},
        {Text: 'Lion', 'score': 2}
      ]
    },
    {
      'questionText': 'Who\'s your favorite instructors?',
      'answers': [
        {Text: 'Max', 'score': 10},
        {Text: 'Max', 'score': 10},
        {Text: 'Max', 'score': 10},
        {Text: 'Max', 'score': 10}
      ]
    },
  ];
  var _questionIndex = 0;
  var _totalScore = 0;

  // void Method for increment questionIndex variable
  void _answerQuestion(int score) {

    _totalScore += score;

//SetState Method used for internal state Changes => State means simply Data or infos used in your App
    setState(() {
      _questionIndex = _questionIndex + 1;
    });

    if (_questionIndex < _questions.length) {
      print("we have more questions");
    } else {
      print("the QCM finish");
    }

    print(_questionIndex);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My First App'),
        ),
        body: _questionIndex < _questions.length
            ? Quiz(
                answerQuestion: _answerQuestion,
                questionIndex: _questionIndex,
                questions: _questions,
              )
            : Result(_totalScore),
      ),
    );
  }
}

here the Error Message

enter image description here

I tried to resolve this issue for over than a week from now, I wish that someone can help me.

Upvotes: 0

Views: 1124

Answers (1)

num8er
num8er

Reputation: 19372

Seems like You've got bad habits from JS world.

The problem is because You use Text without quotes - which most JS developers don't use it when notating objects.

Dart is not JS, TS and etc.

You've to put quotes around keys.

So replace:

{Text: 'Black', 'score': 1},

To:

{'Text': 'Black', 'score': 1},

P.S. In Flutter Text is a class

Upvotes: 1

Related Questions