Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dart Flutter _CastError (Null check operator used on a null value) listyukle Error

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:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

_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(() {
    });
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading