Reputation: 108
I am a beginner in dart and I am trying to make a quiz game application where I use some widgets to get questions and answer options on the screen. The error that I am facing is "The argument type 'List' can't be assigned to the parameter type 'String'" Here are some snippets of my code:-
var questions = [
{
'questionText': 'What is your favourite season?',
'answers': [
'Summer',
'Monsoon',
'Winter',
]
},
{
'questionText': 'What is your favourite color?',
'answers': [
'Black',
'Green',
'Blue',
]
},
{
'questionText': 'What is your favourite food?',
'answers': [
'Chicken',
'Kebab',
'Biryani',
]
},
];
body: Column(
children: [
Question(questions[_questionsIndex]["questionText"]),
...{questions[_questionsIndex]['answers'] as List<String>}
.map((answer) {
return Answer(_answerQuestion, answer); //**ERROR**
}).toList(),
],
),
The error appears on the statement : return Answer(_answerQuestion, answer);
class Answer extends StatelessWidget {
final Function selectAnswer;
final String answerText;
Answer(this.selectAnswer, this.answerText);
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.blue,
elevation: 0.0,
),
child: Text(answerText,
style: TextStyle(
color: Colors.black,
)),
onPressed: () {
selectAnswer();
},
),
);
}
}
What is the meaning of this error and why does it appear? How can I fix this error?
Thank you!
Upvotes: 2
Views: 13800
Reputation: 1
Instead of using List change it to var, it worked for me and the error disappeared is dynamic and works perfectly. This is my code below:
class _QuizPageState extends State<QuizPage> {
var scoreKeeper = [
Icon(
Icons.check,
color: Colors.green,
),
Icon(
Icons.close,
color: Colors.red,
),
Icon(
Icons.close,
color: Colors.red,
),
Icon(
Icons.check,
color: Colors.green,
),
Icon(
Icons.close,
color: Colors.red,
),
];
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
'This is where the question text will go.',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child: TextButton(
onPressed: (){
},
child: Text('True'),
// color: Colors.white,
style: TextButton.styleFrom(
backgroundColor: Colors.white,
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child:TextButton(
onPressed: (){
},
// color: Colors.white,
style: TextButton.styleFrom(
backgroundColor: Colors.pinkAccent,
),
child: const Text('False'),
),
),
),
Row(
children: scoreKeeper,
),
],
);
}
}
Upvotes: 0
Reputation: 443
if you mean: The argument type 'List<String?>?' can't be assigned to the parameter type 'List<String?>'.
I solve this issue with a single line, you can get List<String?> from List<String?>?
List<String?>? mylist = ['data1','data2'];
List<String?> mylist2 = List<String>.from(mylist);
Upvotes: 4
Reputation: 1
You could try especifying the type of your main list with List<Map<String,dynamic>>
List<Map<String,dynamic>> questions = [
{
'questionText': 'What is your favourite season?',
'answers': [
'Summer',
'Monsoon',
'Winter',
]
},
{
'questionText': 'What is your favourite color?',
'answers': [
'Black',
'Green',
'Blue',
]
},
{
'questionText': 'What is your favourite food?',
'answers': [
'Chicken',
'Kebab',
'Biryani',
]
},
];
I tried to assing the element to a List<String>
and every works
var a = questions[1]['answers'];
List<String> b = a;
print(b);
Upvotes: 0
Reputation: 1280
I think the problem is in your second code block. For a list of string, you should use just List
type Instead of List<String>
type.
You code would be like:
body: Column(
children: [
Question(questions[_questionsIndex]["questionText"]),
...{questions[_questionsIndex]['answers'] as List}
.map((answer) {
return Answer(_answerQuestion, answer); //**ERROR**
}).toList(),
],
),
I hope this can solve your problem.
Upvotes: 0