flutter 如何在冻结的联合类上使用copyWith,它实现了特定的混合

az31mfrm  于 2023-05-23  发布在  Flutter
关注(0)|答案(1)|浏览(169)

假设我从文档中有这个类:

abstract class GeographicArea {
  int get population;
  String get name;
}

@freezed
class Example with _$Example {
  const factory Example.person(String name, int age) = Person;

  @Implements<GeographicArea>()
  const factory Example.city(String name, int population) = City;
}

我有一个Example类型的对象,我如何检查该示例是否实现了Geographicarea并将其复制为Interfacspecific属性?

var example = Example.city();
//How to check if this instance implements GeographicArea and call copyWith with a GeographicArea specific parameter?
example = example.copyWith(population: 20000);
bsxbgnwa

bsxbgnwa1#

你可以用'is'关键字来检查它

var example = Example.city();
if(example is GeographicArea){
     *// TODO Type toy logic here* 
}

相关问题