类型'List <Map <Object,Object >>'不是类型转换中'List <Map <String,Object >>'类型的子类型

时间:2020-06-14 00:08:32

标签: list flutter dart casting

我正在观看有关使用VSCode作为IDE学习Flutter和Dart的教程。 在运行我的应用程序时,出现强制转换错误:类型'List >'不是类型转换中类型'List >'的子类型

我的应用程序是测验应用程序

这是quizz.dart文件中的Widget:

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()
      ],
    );
}

这是main.dart文件:

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),
      ),
    );
  }
}

此处是错误消息

enter image description here

从现在开始,我已经尝试解决这个问题超过一个星期,希望有人能帮助我。

1 个答案:

答案 0 :(得分:1)

似乎您从JS世界中有坏习惯。

问题是因为您使用的Text不带引号-大多数JS开发人员在标记对象时都不使用它。

Dart不是JS,TS等

您必须周围加上引号

所以替换:

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

收件人:

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

P.S。在Flutter中,Textclass