json - 通过返回类型的请求的通用 Json 反序列化器
问题描述
我一直在寻找解决方案,但找不到任何解决方案。
基本上我有一组解析 JSON 的模型,这很容易。
我希望能够发出诸如 'fetch(url)' 之类的请求并从请求中获取 T 作为类型。
例如
abstract class Json<T> {
static fromJson(Map<String, dynamic> json) {}
Map<String, dynamic> toJson();
}
我不能在静态中包含 T ,所以不知道该怎么做。
JSON 示例
@JsonSerializable()
class AppConfig implements Json<AppConfig> {
}
我无法弄清楚,也不知道在飞镖中是否有可能是;我们如何使用泛型返回默认类。例如:
Future<Map<String, dynamic>> fetch({
required String url,
Map<String, dynamic> body = const {},
}) async {
final response = await http.post(uri, headers: headers, body: body).timeout(Duration(seconds: 30));
responseBody = T.fromJson(response.body); // << Would return the type.
解决方案
推荐阅读
- mysql - MySQL 性能问题:选择查询需要永远
- r - 从前一个 df 创建一个新的 df,然后用 R 将 2 列合二为一
- android - 如何设计带有实时移动摄像头的 VR 应用程序来检测事物?
- mysql - 查询在 mysql 上运行,但在放入查询检查器时失败
- c++ - 将 CBitmap 转换为 HICON 并在状态栏上使用它
- java - 在 Android Studio 中的自定义 AlertDialog 顶部覆盖图像
- java - 使用 FFMPEG 过滤器模拟着色器结果
- c# - 使用 Web 服务来显示列表 xamarin 表单
- android - Android Studio 在每次构建时下载 .pom 插件
- c - 我应该研究哪个函数来在 C 中创建一个套接字?