so I am currently learning Dart (Flutter) and I have come to something I can't really understand. So I'm hoping that someone will be able to point out the obvious thing here :)
If I write my code like this:
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: QuizPage(
title: 'Quiz APP',
questions: [
{'question': 'Color?', 'answers': ["Red", "Blue", "Green", "Black"]},
{'question': 'Animal?', 'answers': ["Cat", "Dog", "Snake", "Rabbit"]},
]),
);
}
}
class QuizPage extends StatefulWidget {
QuizPage({super.key, required this.title, required this.questions});
final String title;
final List<Map<String, Object>> questions;
@override
State<StatefulWidget> createState() {
return _QuizPageSate();
}
}
It works, but I have later to cast the answers Object with as List<String>. However, if I directly define the questions type as:
final List<Map<String, List<String>>> questions;
it will not work and throw an error:
The element type 'String' can't be assigned to the map value type 'List<String>'.
It seems like it doesn't know how to interpret the type I pass in. Do all non "primitive" types inside a Map have to be defined as Object or am I missing something fundamental here?