73 lines
1.6 KiB
Dart
73 lines
1.6 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'dart:developer';
|
|
|
|
class FFConvert {
|
|
FFConvert._();
|
|
// ignore: prefer_function_declarations_over_variables
|
|
static T? Function<T extends Object?>(dynamic value) convert = <T>(dynamic value) {
|
|
if (value == null) {
|
|
return null;
|
|
}
|
|
return json.decode(value.toString()) as T?;
|
|
};
|
|
}
|
|
|
|
T? asT<T extends Object?>(dynamic value, [T? defaultValue]) {
|
|
if (value is T) {
|
|
return value;
|
|
}
|
|
try {
|
|
if (value != null) {
|
|
final String valueS = value.toString();
|
|
if ('' is T) {
|
|
return valueS as T;
|
|
} else if (0 is T) {
|
|
return int.parse(valueS) as T;
|
|
} else if (0.0 is T) {
|
|
return double.parse(valueS) as T;
|
|
} else if (false is T) {
|
|
if (valueS == '0' || valueS == '1') {
|
|
return (valueS == '1') as T;
|
|
}
|
|
return (valueS == 'true') as T;
|
|
} else {
|
|
return FFConvert.convert<T>(value);
|
|
}
|
|
}
|
|
} catch (e, stackTrace) {
|
|
log('asT<$T>', error: e, stackTrace: stackTrace);
|
|
return defaultValue;
|
|
}
|
|
|
|
return defaultValue;
|
|
}
|
|
|
|
class ResponseModel {
|
|
ResponseModel({
|
|
this.code,
|
|
this.message,
|
|
this.data,
|
|
});
|
|
|
|
factory ResponseModel.fromJson(Map<String, dynamic> jsonRes) => ResponseModel(
|
|
code: asT<int?>(jsonRes['code']),
|
|
message: asT<String?>(jsonRes['message']),
|
|
data: asT<dynamic>(jsonRes['data']),
|
|
);
|
|
|
|
int? code;
|
|
String? message;
|
|
dynamic data;
|
|
|
|
@override
|
|
String toString() {
|
|
return jsonEncode(this);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() => <String, dynamic>{
|
|
'code': code,
|
|
'message': message,
|
|
'data': data,
|
|
};
|
|
} |