flutter - dio http get error ArgumentError (Invalid argument(s) (input): Must not be null) flutter
问题描述
我在这里遇到错误,可能是什么问题?
if (input == null) throw new ArgumentError.notNull('input'); ArgumentError(无效参数(输入):不能为空)
有人能帮我一下吗?
尝试使用 DIO 颤振包并面临 get 调用的问题,我在某处失踪,不确定是什么问题。
国家管理是扑河港。
服务文件:
Future<List<DefaultModel>> getDefaults() async {
try {
final response =
await _dio.get("https://5f279ddaf5d27e001612e29b.mockapi.io/Default");
print(response);
if (response.statusCode == 200) {
final results = List<Map<String, dynamic>>.from(response.data);
List<DefaultModel> defaults = results
.map((defaulteData) => DefaultModel.fromMap(defaulteData))
.toList(growable: false);
return defaults;
} else {
return null;
}
} on DioError catch (e) {
throw GeneralException.fromDioError(e);
}
}
添加模型文件:
import 'dart:convert';
class DefaultModel {
String id;
DateTime createdAt;
String description;
String memberId;
String departmentId;
String reason;
String taskPriority;
String impactToMission;
String stepsToAvoid;
DateTime defaultDate;
DateTime updatedDate;
String hodPrayschit;
String hodComments;
DateTime hodDefaultDeadline;
DateTime sevakAcknowledgeDate;
String hodId;
String hrdComments;
String maulikjiComments;
String cardType;
DateTime startDate;
DateTime endDate;
DefaultModel(
{this.id,
this.createdAt,
this.description,
this.memberId,
this.departmentId,
this.reason,
this.taskPriority,
this.impactToMission,
this.stepsToAvoid,
this.defaultDate,
this.updatedDate,
this.hodPrayschit,
this.hodComments,
this.hodDefaultDeadline,
this.sevakAcknowledgeDate,
this.hodId,
this.hrdComments,
this.maulikjiComments,
this.cardType,
this.startDate,
this.endDate});
Map<String, dynamic> toMap() {
return {
'id': id,
'createdAt': createdAt.toString(),
'description': description,
'memberId': memberId,
'departmentId': departmentId,
'reason': reason,
'taskPriority': taskPriority,
'impactToMission': impactToMission,
'stepsToAvoid': stepsToAvoid,
'defaultDate': defaultDate.toString(),
'updatedDate': updatedDate.toString(),
'hodPrayschit': hodPrayschit,
'hodComments': hodComments,
'hodDefaultDeadline': hodDefaultDeadline.toString(),
'sevakAcknowledgeDate': sevakAcknowledgeDate.toString(),
'hodId': hodId,
'hrdComments': hrdComments,
'maulikjiComments': maulikjiComments,
'cardType': cardType,
'startDate': startDate.toString(),
'endDate': endDate.toString(),
};
}
factory DefaultModel.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return DefaultModel(
id: map['id'],
createdAt: DateTime.parse(map['created']),
description: map['description'],
memberId: map['memberId'],
departmentId: map['departmentId'],
reason: map['reason'],
taskPriority: map['taskPriority'],
impactToMission: map['impactToMission'],
stepsToAvoid: map['stepsToAvoid'],
defaultDate: DateTime.parse(map['defaultDate']),
updatedDate: DateTime.parse(map['updatedDate']),
hodPrayschit: map['hodPrayschit'],
hodComments: map['hodComments'],
hodDefaultDeadline: DateTime.parse(map['hodDefaultDeadline']),
sevakAcknowledgeDate: DateTime.parse(map['sevakAcknowledgeDate']),
hodId: map['hodId'],
hrdComments: map['hrdComments'],
maulikjiComments: map['maulikjiComments'],
cardType: map['cardType'],
startDate: DateTime.parse(map['startDate']),
endDate: DateTime.parse(map['endDate']),
);
}
String toJson() => json.encode(toMap());
List<DefaultModel> defaultFromJson(String jsonData) {
final data = json.decode(jsonData);
return List<DefaultModel>.from(
data.map((item) => DefaultModel.fromJson(item)));
}
factory DefaultModel.fromJson(String source) =>
DefaultModel.fromMap(json.decode(source));
}
页面飞镖
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/all.dart';
import 'package:srmd_timesheet_flutter_app/models/default_model.dart';
import 'package:srmd_timesheet_flutter_app/utils/exception.dart';
import 'package:srmd_timesheet_flutter_app/widgets/app_bar_widget.dart';
import 'package:srmd_timesheet_flutter_app/widgets/badge_layout_widget.dart';
import 'package:srmd_timesheet_flutter_app/services/default_service.dart';
import 'package:srmd_timesheet_flutter_app/widgets/error_body_widget.dart';
final defaultFutureProvider =
FutureProvider.autoDispose<List<DefaultModel>>((ref) async {
ref.maintainState = true;
final defaultService = ref.watch(defaultServiceProvider);
final prayschits = await defaultService.getDefaults();
return prayschits;
});
class AllDefaultPage extends ConsumerWidget {
final String yellowCard = "yellow-card@3x.png";
final String redCard = "red-card@3x.png";
@override
Widget build(BuildContext context, ScopedReader watch) {
return Scaffold(
appBar: MyAppBar(
context,
titleText: "PAST DEFAULTS",
isChipLayout: false,
),
body: watch(defaultFutureProvider).when(
error: (e, s) {
if (e is GeneralException) {
return ErrorBody(message: e.message);
}
return ErrorBody(message: "Oops, something unexpected happened");
},
loading: () => Center(
child: CircularProgressIndicator(),
),
data: (prayschits) {
return RefreshIndicator(
onRefresh: () {
return context.refresh(defaultFutureProvider);
},
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(22.0),
child: Container(
child: Column(
children: prayschits
.map((prayschit) => BadgeLayout(cardImage: redCard))
.toList(),
),
),
),
),
);
},
),
);
}
}
// SizedBox(height: 18.0),
解决方案
ArgumentError 是一个运行时错误,因为其中一个类属性与 Json 中的不同。我更喜欢使用代码生成库来使用序列化 JSON。
解决方案:使用as运算符来了解导致错误的值类型。
最后,使用Null 检查使所有属性成为可选
class Data {
final String name;
Data(this.name);
factory Data.fromJson(Map<String, dynamic> json) {
return Data(
id: json['name'] == null ? null : json['name'] as String,
);}
推荐阅读
- bash - 使用 bash 自动化名册
- python - 设置循环以检测 discord.py 中的按键
- android - Firestore 获取文档中随机命名的每个字段
- angular - 无法使用传单搜索插件和角度传单指令添加搜索控制
- elasticsearch - 如何在 apache lucene 的 TokenFilter 类中直接访问和更新令牌?
- android-toolbar - 如何在没有操作栏的应用程序中使用 Mapbox PlacePicker 活动?
- mysql - 变量 AS (SELECT...) SQL
- c# - 如何在 C# 中使用具有返回类型而不指定它的泛型函数?
- c# - 如何在 JSON.NET 中读取 json 对象值内的长值数组
- excel - 删除包含日期和月份但忽略年份的行