【问题标题】:type 'Null' is not a subtype of type 'List<dynamic>' in type cast类型“Null”不是类型转换中“List<dynamic>”类型的子类型
【发布时间】:2021-12-06 09:52:24
【问题描述】:

请任何人解释这是什么问题!我收到此错误 “类型‘Null’不是类型转换中‘List’类型的子类型”

&

“无效参数(onError):Future.catchError 的错误处理程序必须返回未来类型的值”

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);
  }

颤振医生-v

[✓] Flutter (Channel stable, 2.5.2, on macOS 11.6 20G165 darwin-arm, locale en-DE) • Flutter 版本 2.5.2,位于 /Users/almamun/Documents/developer/flutter • 上游仓库https://github.com/flutter/flutter.git • 框架修订 3595343e20(3 周前),2021-09-30 12:58:18 -0700 • 引擎修订版 6ac856380f • Dart 版本 2.14.3

[✓] Android 工具链 - 为 Android 设备开发(Android SDK 版本 31.0.0) • Android SDK 位于 /Users/almamun/Library/Android/sdk • 平台 android-31,构建工具 31.0.0 • Java 二进制文件位于:/Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java 版 OpenJDK 运行时环境(内部版本 11.0.10+0-b96-7249189) • 接受所有 Android 许可证。

[!] Xcode - 为 iOS 和 macOS 开发 • Xcode 位于 /Applications/Xcode.app/Contents/Developer • Xcode 13.0,内部版本 13A233 ✗ 未安装 CocoaPods。 CocoaPods 用于检索 iOS 和 macOS 平台端的插件代码,该代码响应您在 飞镖侧。 如果没有 CocoaPods,插件将无法在 iOS 或 macOS 上运行。 欲了解更多信息,请参阅https://flutter.dev/platform-plugins 要安装,请参阅 https://guides.cocoapods.org/using/getting-started.html#installation 以获取说明。

[✓] Chrome - 为网络开发 • Chrome 位于 /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio(版本 2020.3) • Android Studio 位于 /Applications/Android Studio.app/Contents • Flutter 插件可以从以下位置安装: ??? https://plugins.jetbrains.com/plugin/9212-flutter • Dart 插件可以从以下位置安装: ??? https://plugins.jetbrains.com/plugin/6351-dart • Java 版 OpenJDK 运行时环境(内部版本 11.0.10+0-b96-7249189)

[✓] VS 代码(版本 1.60.2) • VS 代码位于 /Users/almamun/Downloads/Visual Studio Code.app/Contents • Flutter 扩展可以从以下位置安装: ??? https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] 已连接设备(2 个可用) • sdk gphone arm64(移动) • emulator-5554 • android-arm64 • Android 11 (API 30)(模拟器) • Chrome(网络) • chrome • web-javascript • Google Chrome 94.0.4606.81

【问题讨论】:

    标签: firebase flutter dart


    【解决方案1】:

    问题是您调用运算符is 并将值转换为List 类型,除了此值是null,它不是List 的子类型,因此出现错误。

    在尝试将其转换为另一种类型(如is List)之前,您必须先检查您的值是否不是null

    试试这个:

    ((data['softskills'] ?? []) as List)
    

    【讨论】:

    • 谢谢,但它显示此错误“状态不佳:无元素”
    • 因为您尝试访问 .first 在一个空列表中...您必须检查长度是否 > 0 或提供具有某些值的默认列表,例如: ((data['softskills'] ? ? ['hello']) as List)
    【解决方案2】:

    我认为data['softskills'] as List 是错误的原因。也许data['softskills']null 并且您正在使用它作为列表。所以你应该尝试测试它是否为空。

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 2021-10-20
      • 2019-12-31
      • 2021-10-23
      • 2020-12-21
      • 2021-05-08
      • 2019-01-13
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多