5

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

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

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 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';
4

1 回答 1

3

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

于 2021-12-11T09:01:52.840 回答