I have a code like this:
void saveList() async {
final prefences = await SharedPreferences.getInstance();
prefences.setStringList("tests", prefences.getStringList("tests")! + ["English / A1 / Family / Test 1"]);
setState(() {
});
}
I’m calling the saveList function somewhere. When I call it, I get an error like this:
_CastError (Null check operator used on a null value) saveList
It gives the error on this line:
prefences.setStringList("tests", prefences.getStringList("tests")! + ["Ingilizce / A1 / Aile / Test 1"]);
How can I solve it? Thank you for your help.
>Solution :
I think the first time you call this line, "tests" has never been set before in prefences, then prefences.getStringList("tests") is null.
Try this to fix it:
void saveList() async {
final prefences = await SharedPreferences.getInstance();
List<String> previousList = prefences.getStringList("tests") ?? [];
prefences.setStringList("tests", previousList + ["English / A1 / Family / Test 1"]);
setState(() {
});
}
