I am getting an error here, what could be the issue?
if (input == null) throw new ArgumentError.notNull('input'); ArgumentError (Invalid argument(s) (input): Must not be null)
can anyone help me here?
trying to use DIO flutter package and facing an issue with the get call, i am missing somewhere, not sure what is the issue.
state management is flutter riverpord.
Service File:
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);
}
}
Adding Model File:
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));
}
Page.dart
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 is a runtime error because one of the class properties is not the same as the one from Json. I prefer to use Serializing JSON using code generation libraries.
Solution : use as operator to know which value type was causing the error.
finally, make all properties optional by using Null checking
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,
);}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.