im trying to put the current answer as a hint text when it’s available. but i’m getting this error: NoSuchMethodError: The getter 'isNotEmpty' was called on null.
any ideas on how to fix it?
final Map<String, String> answers = {};
final Map<String, String> qst = {
"Everyone should read...": "",
"Two truths and a lie...": "",
"I can quote every line from...": "",
"If I didn't have to work I would...": "",
"People think I am...": "",
"Never have I ever...": "",
"Believe it or not, I...": "",
"I am amazing at...": "",
"My life as a movie...": "",
"My ultimate dinner party guest list...": "",
"The dorkiest thing about me is...": "",
"On the weekend you'll find me...": "",
};
Future<void> _write(Map<String, dynamic> map) async {
final directory = await getApplicationDocumentsDirectory();
final jsonStr = jsonEncode(map);
final file = File('${directory.path}/answers.txt');
await file.writeAsString(jsonStr);
}
List<Data> data = [];
@override
void initState() {
super.initState();
read();
}
Future<Map<String, dynamic>> read() async {
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/answers.txt');
final jsonStr = await file.readAsString();
final raw = jsonDecode(jsonStr) as Map<String, dynamic>;
raw.forEach((key, value) {
data.add(Data(question: key, answer: value));
});
print(raw);
return raw;
}
@override
Widget build(
BuildContext context,
) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
for (var q in qst.keys) question(q),
SizedBox(
height: 50,
),
],
);
}
Widget question(String question) {
final myController = TextEditingController();
return Padding(
padding: const EdgeInsets.only(top: 15),
child: Stack(
children: [
Card(
elevation: 0,
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 15, left: 10),
child: Text(
question,
style: TextStyle(
color: Colors.black,
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
),
TextField(
onChanged: (String ansr) {
answers = ansr;
},
onSubmitted: (String ansr) {
_write(answers);
},
controller: myController,
decoration: InputDecoration(
hintText: answers.isNotEmpty
? answers
: "write something",
),
),
],
),
),
),
],
),
);
}
}
should i use some state management or is there any way to do it another way? {this’s a random text so stackoverflow accept my question because it says my question is mostly code and I need to add some more details}
>Solution :
In Dart, when accessing values from a map, the returned value could be either Null or the actual value stored in the map. In your case, at the moment you try to access the answers, there is no value – null is returned. Then, you try to call the isNotEmpty on null, you get the error.
To fix this, replace this:
decoration: InputDecoration(
hintText: answers.isNotEmpty
? answers
: "write something",
),
with this:
decoration: InputDecoration(
hintText: answers?.isNotEmpty ?? false
? answers
: "write something",
),
In this example, if answers is null, the statement will fall under the false condition, your code won’t break.
For more info, check null safety in Dart.