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!
CodePudding user response:
Because of the type :
List<Map<String,List<Map<String,int>>>>
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 :
List<Map<String, dynamic>>