颤音:type' list< string,object>>>'不是类型的子类型' list< string,int>>>'在类型中
在使用flutter
和vs code
作为IDE构建测验应用程序的AA原型期间,我收到以下错误:
键入'list'list< map< string,object&gt ;>'不是类型的“列表”< map< string,int>>' 在类型中cast
在我的quiz.dart
文件中,
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import './answer.dart';
import './question.dart';
class Quiz extends StatelessWidget {
final Function? fun;
final int? questionIndex;
final List<Map<String, Object>>? array;
Quiz({required this.fun, required this.questionIndex, required this.array});
@override
Widget build(BuildContext context) {
return Column(
children: [
Question(
(array)![questionIndex!]['questionText'] as String,
),
...((array)![questionIndex!]['answers'] as List<Map<String, int>>)
.map((question) {
return Answer((() => (fun!(question['score'])) as VoidCallback),
(question['text'] as String));
}).toList(),
// ...[Text('hello'), Text('okay')],
],
);
}
}
,以下是我的main.dart
文件,其中一个人可以找到我的插件array
>在测验
widget类的构造函数中。
import 'package:flutter/material.dart';
import './quiz.dart';
import './result.dart';
// void main() {
// runApp(MyApp());
// }
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
final _ques = const [
{
'questionText': 'What\'s your fav. color?',
'answers': [
{'text': 'Black', 'score': 10},
{'text': 'Red', 'score': 5},
{'text': 'Green', 'score': 3},
{'text': 'White', 'score': 1},
]
},
{
'questionText': 'What\'s your fav. animal?',
'answers': [
{'text': 'Cat', 'score': 5},
{'text': 'Dog', 'score': 3},
{'text': 'Snake', 'score': 4},
{'text': 'Bird', 'score': 2}
]
},
{
'questionText': 'Who\'s your fav. instructor?',
'answers': [
{'text': 'Max', 'score': 1},
{'text': 'Max', 'score': 1},
{'text': 'Max', 'score': 1}
]
},
];
var _questionIndex = 0;
var _totalScore = 0;
void _hola(int score) {
_totalScore += score;
setState(() => _questionIndex = _questionIndex + 1);
print(_questionIndex);
if (_questionIndex < _ques.length) {
print('We have more questions!!');
}
}
@override
Widget build(BuildContext context) {
// print('hpllll ${context}');
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Ayoo App Bar stuff'),
backgroundColor: Colors.black,
),
body: _questionIndex < _ques.length
? Quiz(questionIndex: _questionIndex, array: _ques, fun: _hola)
: Result(_totalScore),
),
);
}
}
实际上,即使我用来
final List<Map<String,List<Map<String,int>>>> _ques = const [
{
'questionText': 'What\'s your fav. color?',
'answers': [
{'text': 'Black', 'score': 10},
{'text': 'Red', 'score': 5},
{'text': 'Green', 'score': 3},
{'text': 'White', 'score': 1},
]
},
{
'questionText': 'What\'s your fav. animal?',
'answers': [
{'text': 'Cat', 'score': 5},
{'text': 'Dog', 'score': 3},
{'text': 'Snake', 'score': 4},
{'text': 'Bird', 'score': 2}
]
},
{
'questionText': 'Who\'s your fav. instructor?',
'answers': [
{'text': 'Max', 'score': 1},
{'text': 'Max', 'score': 1},
{'text': 'Max', 'score': 1}
]
},
];
明确定义datatype
_ques
list,我也会收到错误。以下是一个屏幕截图显示错误
在修复/解释此错误方面的任何帮助将受到高度赞赏!
During construction of a a prototype for a quiz app using Flutter
and VS Code
as the IDE, I got the following error:
type 'List<Map<String, Object>>' is not a subtype of type 'List<Map<String, int>>' in type cast
from my quiz.dart
file which is
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import './answer.dart';
import './question.dart';
class Quiz extends StatelessWidget {
final Function? fun;
final int? questionIndex;
final List<Map<String, Object>>? array;
Quiz({required this.fun, required this.questionIndex, required this.array});
@override
Widget build(BuildContext context) {
return Column(
children: [
Question(
(array)![questionIndex!]['questionText'] as String,
),
...((array)![questionIndex!]['answers'] as List<Map<String, int>>)
.map((question) {
return Answer((() => (fun!(question['score'])) as VoidCallback),
(question['text'] as String));
}).toList(),
// ...[Text('hello'), Text('okay')],
],
);
}
}
Following is my main.dart
file where one can find my plugin for array
in the constructor of Quiz
widget class.
import 'package:flutter/material.dart';
import './quiz.dart';
import './result.dart';
// void main() {
// runApp(MyApp());
// }
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
final _ques = const [
{
'questionText': 'What\'s your fav. color?',
'answers': [
{'text': 'Black', 'score': 10},
{'text': 'Red', 'score': 5},
{'text': 'Green', 'score': 3},
{'text': 'White', 'score': 1},
]
},
{
'questionText': 'What\'s your fav. animal?',
'answers': [
{'text': 'Cat', 'score': 5},
{'text': 'Dog', 'score': 3},
{'text': 'Snake', 'score': 4},
{'text': 'Bird', 'score': 2}
]
},
{
'questionText': 'Who\'s your fav. instructor?',
'answers': [
{'text': 'Max', 'score': 1},
{'text': 'Max', 'score': 1},
{'text': 'Max', 'score': 1}
]
},
];
var _questionIndex = 0;
var _totalScore = 0;
void _hola(int score) {
_totalScore += score;
setState(() => _questionIndex = _questionIndex + 1);
print(_questionIndex);
if (_questionIndex < _ques.length) {
print('We have more questions!!');
}
}
@override
Widget build(BuildContext context) {
// print('hpllll ${context}');
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Ayoo App Bar stuff'),
backgroundColor: Colors.black,
),
body: _questionIndex < _ques.length
? Quiz(questionIndex: _questionIndex, array: _ques, fun: _hola)
: Result(_totalScore),
),
);
}
}
In fact, I get an error even when I use
final List<Map<String,List<Map<String,int>>>> _ques = const [
{
'questionText': 'What\'s your fav. color?',
'answers': [
{'text': 'Black', 'score': 10},
{'text': 'Red', 'score': 5},
{'text': 'Green', 'score': 3},
{'text': 'White', 'score': 1},
]
},
{
'questionText': 'What\'s your fav. animal?',
'answers': [
{'text': 'Cat', 'score': 5},
{'text': 'Dog', 'score': 3},
{'text': 'Snake', 'score': 4},
{'text': 'Bird', 'score': 2}
]
},
{
'questionText': 'Who\'s your fav. instructor?',
'answers': [
{'text': 'Max', 'score': 1},
{'text': 'Max', 'score': 1},
{'text': 'Max', 'score': 1}
]
},
];
to explicitly define the datatype
for _ques
list. Following is a screenshot showing the error
Any help in regard to fixing/explaining this error is highly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于类型:
您创建了一个地图列表,其中左侧始终是字符串,右侧必须始终是映射列表&lt; string,int&gt;。
我建议改用以下类型:
Because of the type :
You created a list of map where the left Side are always String and the Right side must be always List of Map<String, int>.
I suggest to use the following type instead :