feat:增加公共组件
This commit is contained in:
28
lib/utils/convert_utils.dart
Normal file
28
lib/utils/convert_utils.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
import '../models/common_model.dart';
|
||||
|
||||
List<T> convertList<T>(
|
||||
dynamic data,
|
||||
T Function(Map<String, dynamic>) fromJson,
|
||||
) {
|
||||
if (data is List) {
|
||||
return data.map((item) => fromJson(item as Map<String, dynamic>)).toList();
|
||||
}
|
||||
throw FormatException(
|
||||
'Expected a list of items for conversion, but got ${data.runtimeType}',
|
||||
);
|
||||
}
|
||||
|
||||
PageResult<T> convertPage<T>(
|
||||
dynamic data,
|
||||
T Function(Map<String, dynamic>) fromJson,
|
||||
) {
|
||||
if (data is Map<String, dynamic>) {
|
||||
return PageResult<T>.fromJson(
|
||||
data,
|
||||
(json) => fromJson(json as Map<String, dynamic>),
|
||||
);
|
||||
}
|
||||
throw FormatException(
|
||||
'Expected a Map for page response conversion, but got ${data.runtimeType}',
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user