VSCode show me this problem notification:
Exception has occurred.
NoSuchMethodError (NoSuchMethodError: The method ‘map’ was called on
null. Receiver: null Tried calling: map(Closure: (String) =>
Answer))
this is the main library code:
import 'package:flutter/material.dart';
import './question.dart';
import 'answer.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> {
var questionIndex = 0;
void answerquestion() {
setState(() {
if (questionIndex < 1) {
questionIndex = questionIndex + 1;
}
});
print("answer question!");
}
@override
Widget build(BuildContext context) {
var questions = [
{
"questionText": "where are you from?",
"answers": ["berlin ", "london", "DC", "new york"]
},
{
"questionText": "what is your favourite color?",
"answers": [" purple", "red", "yellow ", "blue"]
}
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("jini question"),
),
body: Column(children: [
Question(
questions[questionIndex]["questionText"],
),
...(questions[questionIndex]["answer"] as List<String>).map((answer) {
return Answer(answerquestion, answer);
}).toList()
]),
),
);
}
}
i am trying to use map function but i got this error message
2
Answers
The value of
returns null. So your call will be like
(null).map { }
Null object doesn’t have any method called map. That’s why you’re getting this error.
In this case, you’re getting the value maybe because of the typo error. You’re using
"answer"
instead of"answers"
.I would recommend you to null check element before using it. There is an article in dart.dev.
As mentioned in the comments you should try replacing
["answer"]
with["answers"]
.