我为案件1设定了额外的模型。
class ApiResponseModel {
final int status_code;
final String? status;
final String? message;
final List<ApiResponseDataModel> data;
ApiResponseModel({
required this.status_code,
this.status,
this.message,
required this.data,
});
ApiResponseModel copyWith({
int? status_code,
String? status,
String? message,
List<ApiResponseDataModel>? data,
}) {
return ApiResponseModel(
status_code: status_code ?? this.status_code,
status: status ?? this.status,
message: message ?? this.message,
data: data ?? this.data,
);
}
Map<String, dynamic> toMap() {
return <String, dynamic>{
status_code : status_code,
status : status,
message : message,
data : data.map((x) => x.toMap()).toList(),
};
}
factory ApiResponseModel.fromMap(Map<String, dynamic> map) {
return ApiResponseModel(
status_code: map[ status_code ] as int,
status: map[ status ] != null ? map[ status ].toString() : null,
message: map[ message ] != null ? map[ message ].toString() : null,
data: List<ApiResponseDataModel>.from(
(map[ data ] as List).map<ApiResponseDataModel>(
(x) => ApiResponseDataModel.fromMap(
x as Map<String, dynamic>), // look what i did here
),
),
);
}
String toJson() => json.encode(toMap());
factory ApiResponseModel.fromJson(dynamic source) =>
ApiResponseModel.fromMap(source as Map<String, dynamic>);
}
class ApiResponseDataModel {
final String userID;
final String emailAddress;
final String fullName;
final String profilePic;
final String referralCode;
final String registerDate;
final String userType;
ApiResponseDataModel({
required this.userID,
required this.emailAddress,
required this.fullName,
required this.profilePic,
required this.referralCode,
required this.registerDate,
required this.userType,
});
ApiResponseDataModel copyWith({
String? userID,
String? emailAddress,
String? fullName,
String? profilePic,
String? referralCode,
String? registerDate,
String? userType,
}) {
return ApiResponseDataModel(
userID: userID ?? this.userID,
emailAddress: emailAddress ?? this.emailAddress,
fullName: fullName ?? this.fullName,
profilePic: profilePic ?? this.profilePic,
referralCode: referralCode ?? this.referralCode,
registerDate: registerDate ?? this.registerDate,
userType: userType ?? this.userType,
);
}
Map<String, dynamic> toMap() {
return <String, dynamic>{
userID : userID,
emailAddress : emailAddress,
fullName : fullName,
profilePic : profilePic,
referralCode : referralCode,
registerDate : registerDate,
userType : userType,
};
}
factory ApiResponseDataModel.fromMap(Map<String, dynamic> map) {
return ApiResponseDataModel(
userID: map[ userID ].toString(),
emailAddress: map[ emailAddress ].toString(),
fullName: map[ fullName ].toString(),
profilePic: map[ profilePic ].toString(),
referralCode: map[ referralCode ].toString(),
registerDate: map[ registerDate ].toString(),
userType: map[ userType ].toString(),
);
}
String toJson() => json.encode(toMap());
factory ApiResponseDataModel.fromJson(dynamic source) =>
ApiResponseDataModel.fromMap(
source as Map<String, dynamic>); // look here, i used dynamic
}
Future<List<ApiResponseModel>> login({required Map payload}) async {
try {
final List<ApiResponseModel> fetchList = [];
final response = await apiService.postRequest(
endpoint: /user/auth/login ,
data: payload,
);
if (response.statusCode == 200) {
final result = json.decode(response.body) as List;
for (var e in result) {
fetchList.add(ApiResponseModel.fromJson(e));
}
} else {}
return fetchList;
} catch (err) {
logger.log("Login Error: $err");
rethrow;
}
}
案件2
class ApiResponseModel {
final int status_code;
final String? status;
final String? message;
final ApiResponseDataModel data;
ApiResponseModel({
required this.status_code,
this.status,
this.message,
required this.data,
});
ApiResponseModel copyWith({
int? status_code,
String? status,
String? message,
ApiResponseDataModel? data,
}) {
return ApiResponseModel(
status_code: status_code ?? this.status_code,
status: status ?? this.status,
message: message ?? this.message,
data: data ?? this.data,
);
}
Map<String, dynamic> toMap() {
return <String, dynamic>{
status_code : status_code,
status : status,
message : message,
data : data.toMap(),
};
}
factory ApiResponseModel.fromMap(Map<String, dynamic> map) {
return ApiResponseModel(
status_code: map[ status_code ] as int,
status: map[ status ] != null ? map[ status ].toString() : null,
message: map[ message ] != null ? map[ message ].toString() : null,
data: ApiResponseDataModel.fromMap(map[ data ] as Map<String, dynamic>),
);
}
String toJson() => json.encode(toMap());
factory ApiResponseModel.fromJson(dynamic source) =>
ApiResponseModel.fromMap(source as Map<String, dynamic>);
}
class ApiResponseDataModel {
final String userID;
final String emailAddress;
final String fullName;
final String profilePic;
final String referralCode;
final String registerDate;
final String userType;
ApiResponseDataModel({
required this.userID,
required this.emailAddress,
required this.fullName,
required this.profilePic,
required this.referralCode,
required this.registerDate,
required this.userType,
});
ApiResponseDataModel copyWith({
String? userID,
String? emailAddress,
String? fullName,
String? profilePic,
String? referralCode,
String? registerDate,
String? userType,
}) {
return ApiResponseDataModel(
userID: userID ?? this.userID,
emailAddress: emailAddress ?? this.emailAddress,
fullName: fullName ?? this.fullName,
profilePic: profilePic ?? this.profilePic,
referralCode: referralCode ?? this.referralCode,
registerDate: registerDate ?? this.registerDate,
userType: userType ?? this.userType,
);
}
Map<String, dynamic> toMap() {
return <String, dynamic>{
userID : userID,
emailAddress : emailAddress,
fullName : fullName,
profilePic : profilePic,
referralCode : referralCode,
registerDate : registerDate,
userType : userType,
};
}
factory ApiResponseDataModel.fromMap(Map<String, dynamic> map) {
return ApiResponseDataModel(
userID: map[ userID ].toString(),
emailAddress: map[ emailAddress ].toString(),
fullName: map[ fullName ].toString(),
profilePic: map[ profilePic ].toString(),
referralCode: map[ referralCode ].toString(),
registerDate: map[ registerDate ].toString(),
userType: map[ userType ].toString(),
);
}
String toJson() => json.encode(toMap());
factory ApiResponseDataModel.fromJson(dynamic source) =>
ApiResponseDataModel.fromMap(
source as Map<String, dynamic>); // look here, i used dynamic
}
Future<ApiResponseModel?> login({required Map payload}) async {
try {
ApiResponseModel? fetchList;
final response = await apiService.postRequest(
endpoint: /user/auth/login ,
data: payload,
);
if (response.statusCode == 200) {
final result = json.decode(response.body);
final item = ApiResponseModel.fromJson(result);
fetchList = item;
} else {}
return fetchList;
} catch (err) {
logger.log("Login Error: $err");
rethrow;
}
}