gpt4 book ai didi

flutter - 卡住构造函数初始值设定项

转载 作者:行者123 更新时间:2023-12-03 08:15:05 24 4
gpt4 key购买 nike

我有这样的模型(简化):

@immutable
class CountryModel {
final String name;
final String isoCode;
final String assetPath;

const CountryModel({
required this.name,
required this.isoCode,
}) : assetPath = 'assets/countries/$isoCode.PNG';
}

现在我决定迁移到 freezed但无法理解如何处理像 assetPath

这样的字段

我有很多模型,其初始值基于构造函数参数

我的卡住模型:

part 'country_model.freezed.dart';

@freezed
class CountryModel with _$CountryModel {

const factory CountryModel({
required String name,
required String isoCode,
}) = _CountryModel;
}

// : assetPath = 'assets/countries/$isoCode.PNG'

如何在这里添加 assetPath 字段?

因此,如果我像这样创建 CountryModel ,那么在迁移到卡住之前

CountryModel(name: 'name', isoCode: 'XX');

assetPath 值应为'assets/countries/XX.PNG'

最佳答案

卡住中似乎没有构造函数初始化器
所以作为一种选择,我以这种方式解决了它:

part 'country_model.freezed.dart';
part 'country_model.g.dart';

@freezed
class CountryModel with _$CountryModel {
const CountryModel._();

const factory CountryModel({
required String name,
required String isoCode,
}) = _CountryModel;

String get assetPath => 'assets/countries/$isoCode.PNG';
}

从我的角度来看,这不是最好的解决方案,我更喜欢使用构造函数初始化程序,但这次不是

关于flutter - 卡住构造函数初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69661843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com