如何在 Dart/Flutter 中使用从 EnumClass 扩展并由 build_runner 生成的类?

Ουι*_*ευα 5 enums dart flutter build-runner

为了将我的 GraphQL 模式转换为 Dart 类,我使用了Ferry包,并使用build_runner.

在我的数据库中,我定义了以下枚举类型:

CREATE TYPE my_schm.currency AS ENUM ('CNY','EUR','PEN','USD');
Run Code Online (Sandbox Code Playgroud)

这是它的翻译(来自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);
}
Run Code Online (Sandbox Code Playgroud)

该类又用于:

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;
....
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试实现以下请求方法(为了清楚起见,省略了一些变量):

  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
  );
Run Code Online (Sandbox Code Playgroud)

value[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'.
Run Code Online (Sandbox Code Playgroud)

根据文档,我只需要为我的任务导入以下文件:

import '../loggedin.data.gql.dart';
import '../loggedin.req.gql.dart';
import '../loggedin.var.gql.dart';
Run Code Online (Sandbox Code Playgroud)

Mic*_*ato 4

您应该能够使用该类GCurrency。你可以吗vars.currency = GCurrency.valueOf(values[5])