'NoSuchMethodError: The getter 'isNotEmpty' was called on null
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[question] = ansr;
},
onSubmitted: (String ansr) {
_write(answers);
},
controller: myController,
decoration: InputDecoration(
hintText: answers[question].isNotEmpty
? answers[question]
: "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}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
