請任何人都可以解釋這是什么問題!我收到此錯誤“型別‘空’不是型別轉換中‘串列’型別的子型別”
&
“無效引數(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,在 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 - 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
uj5u.com熱心網友回復:
問題是您正在呼叫運算子is并將值轉換為 type List,除了此值是null,它不是 的子型別List,因此是錯誤的。
null在嘗試將其轉換為另一種型別(如is List)之前,您必須首先檢查您的值是否不是。
嘗試這個:
((data['softskills'] ?? []) as List)
uj5u.com熱心網友回復:
我認為data['softskills'] as List是錯誤的原因。也許data['softskills']是null,你正在使用它作為串列。所以你應該嘗試測驗它是否為空。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329009.html
