如何使用从 EnumClass 扩展并由 Dart/Flutter 中的 build_runner 生成的 类?
How to work with classes extended from EnumClass and generated by build_runner in Dart/Flutter?
为了将我的 GraphQL 模式转换为 Dart classes,我使用 Ferry 包,我 运行 使用 build_runner
.
在我的数据库中,我定义了以下 enum 类型:
CREATE TYPE my_schm.currency AS ENUM ('CNY','EUR','PEN','USD');
这是它的翻译(来自schema.schema.gql.dart
):
class GCurrency extends EnumClass {
const GCurrency._(String name) : super(name);
static const GCurrency CNY = _$gCurrencyCNY;
static const GCurrency EUR = _$gCurrencyEUR;
static const GCurrency PEN = _$gCurrencyPEN;
static const GCurrency USD = _$gCurrencyUSD;
static Serializer<GCurrency> get serializer => _$gCurrencySerializer;
static BuiltSet<GCurrency> get values => _$gCurrencyValues;
static GCurrency valueOf(String name) => _$gCurrencyValueOf(name);
}
这个 class 又用于:
class GCreateQuoteRequestVarsBuilder
implements
Builder<GCreateQuoteRequestVars, GCreateQuoteRequestVarsBuilder> {
_$GCreateQuoteRequestVars? _$v;
....
_i2.GCurrency? _currency;
_i2.GCurrency? get currency => _$this._currency;
set currency(_i2.GCurrency? currency) => _$this._currency = currency;
....
}
我正在尝试实现以下请求方法(为清楚起见省略了一些变量):
GCreateQuoteRequestReq createQuoteRequest(List<Object> values) => GCreateQuoteRequestReq(
(b) => b
..vars.vehicle = values[0] as String
..vars.body = values[1] as String
..vars.currency = values[5] as GCurrency
);
values[5]有问题,是String类型,需要转换成正确的类型,应该是 GCurrency,但出现此错误:
The name 'GCurrency' isn't a type, so it can't be used in an 'as' expression.
Try changing the name to the name of an existing type, or creating a type with the name 'GCurrency'.
根据文档,我只需要为我的任务导入以下文件:
import '../loggedin.data.gql.dart';
import '../loggedin.req.gql.dart';
import '../loggedin.var.gql.dart';
您应该可以使用 class GCurrency
。你能vars.currency = GCurrency.valueOf(values[5])
吗?
为了将我的 GraphQL 模式转换为 Dart classes,我使用 Ferry 包,我 运行 使用 build_runner
.
在我的数据库中,我定义了以下 enum 类型:
CREATE TYPE my_schm.currency AS ENUM ('CNY','EUR','PEN','USD');
这是它的翻译(来自schema.schema.gql.dart
):
class GCurrency extends EnumClass {
const GCurrency._(String name) : super(name);
static const GCurrency CNY = _$gCurrencyCNY;
static const GCurrency EUR = _$gCurrencyEUR;
static const GCurrency PEN = _$gCurrencyPEN;
static const GCurrency USD = _$gCurrencyUSD;
static Serializer<GCurrency> get serializer => _$gCurrencySerializer;
static BuiltSet<GCurrency> get values => _$gCurrencyValues;
static GCurrency valueOf(String name) => _$gCurrencyValueOf(name);
}
这个 class 又用于:
class GCreateQuoteRequestVarsBuilder
implements
Builder<GCreateQuoteRequestVars, GCreateQuoteRequestVarsBuilder> {
_$GCreateQuoteRequestVars? _$v;
....
_i2.GCurrency? _currency;
_i2.GCurrency? get currency => _$this._currency;
set currency(_i2.GCurrency? currency) => _$this._currency = currency;
....
}
我正在尝试实现以下请求方法(为清楚起见省略了一些变量):
GCreateQuoteRequestReq createQuoteRequest(List<Object> values) => GCreateQuoteRequestReq(
(b) => b
..vars.vehicle = values[0] as String
..vars.body = values[1] as String
..vars.currency = values[5] as GCurrency
);
values[5]有问题,是String类型,需要转换成正确的类型,应该是 GCurrency,但出现此错误:
The name 'GCurrency' isn't a type, so it can't be used in an 'as' expression.
Try changing the name to the name of an existing type, or creating a type with the name 'GCurrency'.
根据文档,我只需要为我的任务导入以下文件:
import '../loggedin.data.gql.dart';
import '../loggedin.req.gql.dart';
import '../loggedin.var.gql.dart';
您应该可以使用 class GCurrency
。你能vars.currency = GCurrency.valueOf(values[5])
吗?