Reputation: 39
I get this error when I attempt to compile:
The element type 'Iterable' can't be assigned to the list type 'Widget'.dartlist_element_type_not_assignable.
My code:
import 'package:flutter/material.dart';
import './question.dart';
import './answer.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _MyAppState();
}
}
class _MyAppState extends State<MyApp> {
var _questionIndex = 0;
void _answerQuestion() {
setState(() {
_questionIndex = _questionIndex + 1;
print(_questionIndex);
});
}
@override
Widget build(BuildContext context) {
var questions = [
{
'question': 'what is your favourite color',
'answers': ['Black', 'Red', 'Green', 'white']
},
{
'question': 'what is your favourite song',
'answers': ['Thunderstorm', 'AC/DC', 'Mama mia', 'Burriqio']
},
{
'question': 'what is your favourite food',
'answers': ['Macarrones', 'Salmorejo', 'Albondigas', 'Chorizo']
}
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App'),
),
body: Column(
children: [
Question(
questions[_questionIndex]['question'],
),
...(questions[_questionIndex]['answers'] as List<String>).map((answer) {
return Answer(_answerQuestion, answer);
})
],
),
),
);
}
}
I don't understand why I have to use that for add to list. Because isn't the operator for creating a new list with each element mapping with the method map?
Thank you if you can help me.
Upvotes: 0
Views: 58
Reputation: 702
If you don't use spread operator(...), the result is nested list in your code.
Like below.
body: Column(
children: [
Widget1,
[Widget2, Widget3, ...],
],
),
When you use spread operator, the result will be list of Widget.
body: Column(
children: [
Widget1,
...[Widget2, Widget3, ...],
],
),
is same with below.
body: Column(
children: [
Widget1,
Widget2,
Widget3,
...,
],
),
Upvotes: 1