gpt4 book ai didi

list - 类型 'List>'不是类型转换中的 'List>'类型的子类型

转载 作者:行者123 更新时间:2023-12-03 04:45:40 25 4
gpt4 key购买 nike

我正在观看有关使用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
从现在开始,我已尝试解决这个问题超过一个星期,希望有人能帮助我。

最佳答案

似乎您在JS世界中有不良习惯。
问题是因为您使用不带引号的Text-大多数JS开发人员在注释对象时都不使用它。
Dart不是JS,TS等。
您必须周围加上引号
因此,请替换:

{Text: 'Black', 'score': 1},
至:
{'Text': 'Black', 'score': 1},
附言在Flutter中, Textclass

关于list - 类型 'List<Map<Object, Object>>'不是类型转换中的 'List<Map<String, Object>>'类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62366953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com