Home > Enterprise >  Dart Flutter Future Void _CastError (type 'Null' is not a subtype of type 'List<St
Dart Flutter Future Void _CastError (type 'Null' is not a subtype of type 'List<St

Time:03-05

I have such a code. But I am getting error.

enter image description here

   _CastError (type 'Null' is not a subtype of type 'List<String?>' in type cast)

Codes:

Future<List<String?>> listUpload() async {
        final prefences = await SharedPreferences.getInstance();
        final getTests = prefences.getStringList("tests"); // get item
        return Future.value(getTests);
      }

How can I solve it?

CodePudding user response:

The listUpload can possibly return Null depending on the situation. But you have specified a return type of Future<List<String?>>. To make it work try a return type dynamic (not preferred) or check for null before returning a value.

dynamic listUpload() async {
        final prefences = await SharedPreferences.getInstance();
        final getTests = prefences.getStringList("tests"); // get item
        return Future.value(getTests);
      }
  • Related