0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Flutter】スネークケースのAPIレスポンスをキャメルケースで管理する

Last updated at Posted at 2024-02-27

はじめに

こんにちは。Flutter開発2年目のエンジニアです。
とある開発で、APIレスポンスを受け取った時に、返却されているはずのデータがNullで通過する減少に陥りました。
これの対策方法についてご紹介します。

原因

結論、json形式のAPIレスポンスのkeyがスネークケースなのに対して、Flutter側のモデルのパラメータがキャメルケースだったことでNullとなって通過していました。

対策

モデルのパラメータを修正していかないといけないと思ったのですが、調べたらありました。

パッケージのインストール

build.yamlファイルを作成

プロジェクトのルートディレクトリにbuild.yamlファイルを作成

field_renameの設定

build.yaml
targets:
  $default:
    builders:
      json_serializable:
        options:
          field_rename: snake

.gファイルの更新

以下コマンドを叩いて、.gファイルを更新

flutter pub run build_runner build --delete-conflicting-outputs

上記手順で、.gファイルのパラメータ名がスネークケースに変換されたと思います!

最後に

以上、他のオプションも用意されているみたいなので、色々試してみると便利かもしれません。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?