类型“null”不是类型转换中“list dynamic”类型的子类型
Posted
技术标签:
【中文标题】类型“null”不是类型转换中“list dynamic”类型的子类型【英文标题】:type 'null' is not a subtype of type 'list dynamic ' in type cast 【发布时间】:2022-01-19 11:51:20 【问题描述】:请任何人解释这是什么问题!我收到此错误“类型'Null'不是类型转换中'List'类型的子类型”
&
“无效参数(onError):Future.catchError 的错误处理程序必须返回未来类型的值”
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
class AutoGenerate
AutoGenerate(
required this.status,
required this.message,
required this.results,
);
late final int status;
late final String message;
late final List<Results> results;
AutoGenerate.fromJson(Map<String, dynamic> json)
status = json['status'];
message = json['message'];
results = List.from(json['results']).map((e)=>Results.fromJson(e)).toList();
Map<String, dynamic> toJson()
final _data = <String, dynamic>;
_data['status'] = status;
_data['message'] = message;
_data['results'] = results.map((e)=>e.toJson()).toList();
return _data;
class Results
Results(
required this.id,
required this.category,
required this.name,
required this.description,
required this.type,
this.image,
this.video,
required this.deliveryTime,
required this.deliveryFund,
required this.maximumFund,
required this.progress,
required this.startTime,
required this.endTime,
this.participants,
required this.createdAt,
required this.updatedAt,
);
late final int id;
late final Category category;
late final String name;
late final String description;
late final int type;
late final String? image;
late final String? video;
late final String deliveryTime;
late final int deliveryFund;
late final int maximumFund;
late final int progress;
late final String startTime;
late final String endTime;
late final List<Participants>? participants;
late final String createdAt;
late final String updatedAt;
Results.fromJson(Map<String, dynamic> json)
id = json['id'];
category = Category.fromJson(json['category']);
name = json['name'];
description = json['description'];
type = json['type'];
image = null;
video = null;
deliveryTime = json['delivery_time'];
deliveryFund = json['delivery_fund'];
maximumFund = json['maximum_fund'];
progress = json['progress'];
startTime = json['start_time'];
endTime = json['end_time'];
participants = null;
createdAt = json['created_at'];
updatedAt = json['updated_at'];
Map<String, dynamic> toJson()
final _data = <String, dynamic>;
_data['id'] = id;
_data['category'] = category.toJson();
_data['name'] = name;
_data['description'] = description;
_data['type'] = type;
_data['image'] = image;
_data['video'] = video;
_data['delivery_time'] = deliveryTime;
_data['delivery_fund'] = deliveryFund;
_data['maximum_fund'] = maximumFund;
_data['progress'] = progress;
_data['start_time'] = startTime;
_data['end_time'] = endTime;
_data['participants'] = participants;
_data['created_at'] = createdAt;
_data['updated_at'] = updatedAt;
return _data;
class Category
Category(
required this.id,
required this.name,
required this.description,
);
late final int id;
late final String name;
late final String description;
Category.fromJson(Map<String, dynamic> json)
id = json['id'];
name = json['name'];
description = json['description'];
Map<String, dynamic> toJson()
final _data = <String, dynamic>;
_data['id'] = id;
_data['name'] = name;
_data['description'] = description;
return _data;
class Participants
Participants(
required this.id,
required this.name,
required this.phone,
required this.avatar,
);
late final int id;
late final String name;
late final String phone;
late final String avatar;
Participants.fromJson(Map<String, dynamic> json)
id = json['id'];
name = json['name'];
phone = json['phone'];
avatar = json['avatar'];
Map<String, dynamic> toJson()
final _data = <String, dynamic>;
_data['id'] = id;
_data['name'] = name;
_data['phone'] = phone;
_data['avatar'] = avatar;
return _data;
class JobsListView extends StatelessWidget
List<Results> result = [];
@override
Widget build(BuildContext context)
return FutureBuilder<List<Results>>(
future: _fetchJobs(),
builder: (context, snapshot)
if (snapshot.hasData)
List<Results>? data = snapshot.data;
return _jobsListView(data);
else if (snapshot.hasError)
return Text("$snapshot.error");
return CircularProgressIndicator();
,
);
Future<List<Results>> _fetchJobs() async
final String response = await rootBundle.loadString('assets/dummy.json');
final productData = await json.decode(response);
var list = productData['result'] as List<dynamic>;
if (productData.statusCode == 200)
return productData.map((job) => Results.fromJson(job)).toList();
else
throw Exception('Failed to load jobs from API');
ListView _jobsListView(data)
return ListView.builder(
itemCount: 1,
scrollDirection:Axis.horizontal,
itemBuilder: (context, index)
return _tile(data[index].name, data[index].description, data[index].progress, data[index].category);
);
Card _tile(String name, String description, String description2,String category) => Card(
child: Text(name),
);
颤振医生-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 • 上游存储库 @987654321 @ • 框架修订版 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 平台端的插件代码,以响应您在 Dart 端的插件使用情况。如果没有 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 Code(版本 1.60.2) • VS Code 位于 /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铬 94.0.4606.81
【问题讨论】:
错误在这一行var list = productData['result'] as List<dynamic>;
确保您的数据不为空或添加条件
【参考方案1】:
在您的 API 调用响应中,您收到一个具有列表数据类型的字段(可能是 results
字段)的空值。第一个错误导致控件被传递给_fetchJobs()
方法的else 语句,然后您在那里不返回任何值。这就是您出现第二个错误的原因。
【讨论】:
以上是关于类型“null”不是类型转换中“list dynamic”类型的子类型的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 未处理的异常:类型“Null”不是类型转换中“List<dynamic>”类型的子类型