12

Please can anyone explain what's the issue with this! I am getting this error "type 'Null' is not a subtype of type 'List' in type cast"

&

"Invalid argument(s) (onError): The error handler of Future.catchError must return a value of the future's type"

factory Talent.fromJson(String userId, dynamic data) {
    try {
      print('This is the data we are looking for $userId $data');
      return Talent(
        id: userId,
        awareOf: data['awareOf'],
        firstName: data['firstName'],
        lastName: data['lastName'],
        aliasName: data['aliasName'],
        useRealName: data['useRealName'] ?? false,
        revealPicture: data['revealPicture'] ?? false,
        imageUrl: data['imageUrl'],
        staticLocation: LocationModel(
          label: data['staticLocation']['label'],
          latitude: data['staticLocation']['latitude'],
          longitude: data['staticLocation']['longitude'],
        ),
        searchRadius: data['searchRadius'] ?? 25,
        willingToMove: data['willingToMove'] ?? false,
        prefersRemote: data['prefersRemote'] ?? false,
        yearsOfExperience: data['yearsOfExperience'] ?? 0,
        salaryExpectation:
        SalaryExpectation.fromJson(data['salaryExpectation']),
        hardskills: data['hardskills'] ?? [],
        softskills: (data['softskills'] as List)?.first?.runtimeType != String
            ? (data['softskills'] as List)
            ?.map((json) => Softskill.fromJson(json))
            ?.toList() ??
            []
            : (data['softskills'] as List)
            ?.map((s) => Softskill.fromJson(json.decode(s)))
            ?.toList() ??
            [],
        studies: (data['studies'] as List)?.first?.runtimeType != String
            ? (data['studies'] as List)
            ?.map((s) => Study.fromJson(Map<String, dynamic>.from(s)))
            ?.toList() ??
            []
            : (data['studies'] as List)
            ?.map((s) => Study.fromJson(json.decode(s)))
            ?.toList() ??
            [],
        apprenticeships:
        (data['apprenticeships'] as List)?.first?.runtimeType != String
            ? (data['apprenticeships'] as List)
            ?.map((s) => Apprenticeship.fromJson(s))
            ?.toList() ??
            []
            : (data['apprenticeships'] as List)
            ?.map((s) => Apprenticeship.fromJson(json.decode(s)))
            ?.toList() ??
            [],
        coronaHelper: data['coronaHelper'] ?? false,
        driverLicense: data['driverLicense'] ?? false,
      );
    } on Exception catch (e) {
      Logger().e("Unable to parse Talent $e");
    }
    return Talent.fromJson(userId, data);
  }

flutter doctor -v

[✓] Flutter (Channel stable, 2.5.2, on macOS 11.6 20G165 darwin-arm, locale en-DE) • Flutter version 2.5.2 at /Users/almamun/Documents/developer/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 3595343e20 (3 weeks ago), 2021-09-30 12:58:18 -0700 • Engine revision 6ac856380f • Dart version 2.14.3

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/almamun/Library/Android/sdk • Platform android-31, build-tools 31.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 13.0, Build version 13A233 ✗ CocoaPods not installed. CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side. Without CocoaPods, plugins will not work on iOS or macOS. For more info, see https://flutter.dev/platform-plugins To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[✓] VS Code (version 1.60.2) • VS Code at /Users/almamun/Downloads/Visual Studio Code.app/Contents • Flutter extension can be installed from: 🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available) • sdk gphone arm64 (mobile) • emulator-5554 • android-arm64 • Android 11 (API 30) (emulator) • Chrome (web) • chrome • web-javascript • Google Chrome 94.0.4606.81

3 Answers 3

21

The problem is that you're calling the operator is and casting a value to the type List, except that this value is null, which is not a subtype of List, thus the error.

You must first check if your value is not null before trying to cast it to another type (like is List).

Try this:

((data['softskills'] ?? []) as List)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you but it's showing this error "Bad state: No element"
because your trying to access .first on an empty list... you have either to check if length > 0 or give a default list with some value like: ((data['softskills'] ?? ['hello']) as List)
0

I think data['softskills'] as List is the cause of the error. Maybe data['softskills'] is null and you are using as List on it. So you should try testing whether it is null or not.

Comments

0

When updating a class and still want to read the old class adapter you should provide the defaultValue parameter for the non-nullable properties in @HiveField for example like that:

@HiveField(1, defaultValue: "")
String localeCode;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.