LoginSignup
2
0

More than 1 year has passed since last update.

Flutter+Amplify+graphqlのcodegenで生成されたmodelがnullsafety対応にならない

Posted at

内容

Amplifyのflutterがnullsafety対応したので、私が開発しているプロジェクトにも適応しようと思い公式見ながら色々やっていたところ、codegenで生成されたmodelがどう見てもnullsafety対応されていない状態(エラーが出る)で困った。

Amplifyのバージョン:5.1.0

ちなみに公式は以下のページを参照しながらやった。
https://docs.amplify.aws/lib/datastore/getting-started/q/platform/flutter

schema.graphql
type Todo @model {
  id: ID!
  name: String!
  description: String
}
実行コマンド
# amplify codegen models
Todo.dart
class Todo extends Model {
  static const classType = const TodoType();
  final String id;
  final String name;
  final String description;

  @override
  getInstanceType() => classType;

  @override
  String getId() {
    return id;
  }

  const Todo._internal(
      {@required this.id, @required this.name, this.description});

  factory Todo(
      {@required String id, @required String name, String description}) {
    return Todo._internal(
        id: id == null ? UUID.getUUID() : id,
        name: name,
        description: description);
  }

  ...

結論

どうもamplifyフォルダの直下にcli.jsonを作成し、そこでnullsafety適用の設定を書く必要があったそうだ。

以下をamplifyフォルダの直下に作成。

amplify/cli.json
{
  "features": {
    "codegen": {
      "useAppSyncModelgenPlugin": true,
      "enableDartNullSafety": true
    }
  }
}

私は「enableDartNullSafety」さえtrueで設定されていれば良いと思っていて、何回やってもnullsafety対応にならないのでAmplifyCliのバグではないかと疑ってました。。。

「enableDartNullSafety」の設定だけでもcodegen時に以下のようなメッセージ出てたので。
(ひっかけじゃない?)

Detected Dart SDK version 2.12.0 or above
Detected feature flag: “enableDartNullSafety : true”
Generating Dart Models with null safety. To opt out of null safe models, turn off the “enableDartNullSafety” feature flag. Learn more: https://docs.amplify.aws/lib/project-setup/null-safety/q/platform/flutter

これでちゃんとnullsafety対応版のmodelが生成されます!

Todo.dart
class Todo extends Model {
  static const classType = const _TodoModelType();
  final String id;
  final String? _name;
  final String? _description;

  @override
  getInstanceType() => classType;

  @override
  String getId() {
    return id;
  }

  String get name {
    try {
      return _name!;
    } catch(e) {
      throw new DataStoreException(DataStoreExceptionMessages.codeGenRequiredFieldForceCastExceptionMessage, recoverySuggestion: DataStoreExceptionMessages.codeGenRequiredFieldForceCastRecoverySuggestion, underlyingException: e.toString());
    }
  }

  String? get description {
    return _description;
  }

  const Todo._internal({required this.id, required name, description}): _name = name, _description = description;

  factory Todo({String? id, required String name, String? description}) {
    return Todo._internal(
      id: id == null ? UUID.getUUID() : id,
      name: name,
      description: description);
  }

  ...
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0