简体   繁体   English

我如何在 dart 中调用泛型类型的方法

[英]How do i invoke method on a generic type in dart

There are several models have a same structure, { type: xxx, settings: xxx}, so i would like to use a parent class "WidgetConfig" with a generic type "T" to implement this, but problem occurs when i add "fromJson" methods.有几个模型具有相同的结构,{类型:xxx,设置:xxx},所以我想使用父 class 具有通用类型“T”的“WidgetConfig”来实现这个,但是当我添加“fromJson”时出现问题“ 方法。 How can i invoke method on a generic type or any other ways to implement this?我如何调用泛型类型的方法或任何其他方法来实现它?

class BannerWidgetViewModel extends ChangeNotifier {
  WidgetConfig<BannerWidgetConfig> config;

  BannerWidgetViewModel(String configJson){
    config = WidgetConfig.fromJson(configJson);
  }
}

class BannerWidgetConfig {
  String imgUrl;
  String padImgUrl;
  String lessonId;

  BannerWidgetConfig.fromJson(json){
    if (json != null) {
      this.imgUrl = json['imgUrl'];
      this.padImgUrl = json['padImgUrl'];
      this.lessonId = json['lessonId'];
    }
  }
}

class WidgetConfig<T> {
  WidgetType type;

  WidgetConfig.fromJson(json){
    if (json != null) {
      this.type = json['type'];
      // this.settings = T.fromJson(json['settings']); // T doesn't have fromJson method
    }
  }
}

then i use a abstract class but still not working.然后我使用摘要 class 但仍然无法正常工作。

abstract class BaseWidgetConfig {
  BaseWidgetConfig.fromJson(dynamic json);
}

class WidgetConfig<T extends BaseWidgetConfig> {
  WidgetType type;
  T settings;

  WidgetConfig.fromJson(json){
    if (json != null) {
      this.type = json['type'];
      this.settings = T.fromJson();
    }
  }
}

code picture代码图片

Directly show the function as a reference.直接显示function作为参考。

send function as a reference here.在这里发送 function 作为参考。

FireStoreHelper.getList<ModelLesson>('grade4', ModelLesson.fromJson);

get the method here.在这里获取方法。

static Future<List<T>> getList<T>(String path, Function fromJson)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在泛型 Dart 方法中对泛型类型 T 实施实施约束? - How to enforce Implements constraint on generic type T in generic Dart method? 如何使用 Flutter MethodChannel 从本机 swift 代码调用 dart 代码中的方法? - How do I use a Flutter MethodChannel to invoke a method in dart code from the native swift code? 抽象——我如何创建一个通用的 static 方法让几种类型在 Dart/Flutter 中流过? - Abstraction - How do I create a generic static method for several types to flow through in Dart/Flutter? 如何从颤振飞镖应用程序调用手机设置? - How do I invoke the phone settings from flutter dart application? 如何在 dart 中获取通用 Object 的主要类型和子类型,例如 Type1<type2> , 我只想要 Type1 和 Type2 分开</type2> - How do I get the main type and subtype of a Generic Object in dart, For ex Type1<Type2>, I just want the Type1 separatly and Type2 separatly 如何在 Dart 中反序列化具有 List 类型的通用 JSON - How to deserialize generic JSON with List type in Dart 如何为 dart 中的 bool 类型创建方法? - How can I create a method for the type bool in dart? 如何创建通用 Dart 库? - How can I create a generic Dart Library? Dart:不覆盖泛型方法具有类似于返回类型的泛型参数类型 - Dart: doesn't override generic method has generic parameter type similar to the return type 我如何将其变成 Dart? Dart & Flutter - How do I make this into Dart? Dart & Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM