Flutter 如何在dart中对泛型类型调用方法

Flutter 如何在dart中对泛型类型调用方法,flutter,dart,Flutter,Dart,有几个模型具有相同的结构,{type:xxx,settings:xxx},所以我想使用父类“WidgetConfig”和泛型类型“T”来实现这一点,但在添加“fromJson”方法时会出现问题。我怎样才能在泛型类型上调用方法,或者以任何其他方式实现它 class BannerWidgetViewModel extends ChangeNotifier { WidgetConfig<BannerWidgetConfig> config; BannerWidgetViewMod

有几个模型具有相同的结构,{type:xxx,settings:xxx},所以我想使用父类“WidgetConfig”和泛型类型“T”来实现这一点,但在添加“fromJson”方法时会出现问题。我怎样才能在泛型类型上调用方法,或者以任何其他方式实现它

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
    }
  }
}
class BannerWidgetViewModel扩展了ChangeNotifier{
WidgetConfig;
BannerWidgetViewModel(字符串configJson){
config=WidgetConfig.fromJson(configJson);
}
}
类BannerWidgetConfig{
字符串imgUrl;
字符串padImgUrl;
线状莱索尼德;
BannerWidgetConfig.fromJson(json){
if(json!=null){
this.imgUrl=json['imgUrl'];
this.padImgUrl=json['padImgUrl'];
this.lessonId=json['lessonId'];
}
}
}
类WidgetConfig{
WidgetType类型;
WidgetConfig.fromJson(json){
if(json!=null){
this.type=json['type'];
//this.settings=T.fromJson(json['settings']);//T没有fromJson方法
}
}
}

然后我使用了一个抽象类,但仍然不工作

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();
    }
  }
}
抽象类BaseWidgetConfig{
BaseWidgetConfig.fromJson(动态json);
}
类WidgetConfig{
WidgetType类型;
T设置;
WidgetConfig.fromJson(json){
if(json!=null){
this.type=json['type'];
this.settings=T.fromJson();
}
}
}

我删除了我的答案,因为很遗憾答案不正确。不过你可以试着用。非常感谢,根据那篇参考文献中的答案,我认为这确实是不可能的