![[Flutter]Chopperを使用したリクエストでリクエストのHeadersに値を追加する方法](https://www.yukiiworks.com/wp-content/uploads/2020/08/flutter.png)
はじめに
Flutter開発にてAPIを使用するのにChopperを利用している場合のリクエスト時にリクエストヘッダにパラメータを追加する方法です。
以外とすんなり出てこなかったのでメモ。
Headers追加方法
今回のケースでは、リクエストヘッダのキーにAuthorization、値にAPIのトークンを付加するサンプルです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class ApiClientCreator { /// クライアント生成 static ChopperClient create() { return ChopperClient( baseUrl: 'https://xxxxxxx.com/api', converter: JsonConverter(), interceptors: [ (Request request) async { return applyHeader(request, 'Authorization', 'Bearer <API Token>'); }, ], ); } } |
ChopperClientのinterceptorsにapplyHeaderを渡します。
applyHeaderの第2引数にキー名、第3引数に値を設定します。
以上です。
![[Flutter]chopper_generator使用時にbuild_runnerのビルドが完了しない問題の解消法](https://www.yukiiworks.com/wp-content/uploads/2020/08/flutter-300x144.png)
![[realm][Android]relam導入時のエラー"configuration with name 'kapt' not found."について](https://www.yukiiworks.com/wp-content/uploads/2019/04/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f36333036302f31363961343966382d623766622d613335612d353264342d6539653934373731383031302e6a706567-150x150.jpg)
![[AltBeacon][Kotlin]Error while saving scan status to file: Cannot delete existing file.エラーについて](https://www.yukiiworks.com/wp-content/uploads/2019/10/android-studio-e1585186990750-150x150.jpg)
![[iOS]Can’t end BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.エラーについて](https://www.yukiiworks.com/wp-content/uploads/2019/09/Xcode-150x150.png)
![[Python]tkinterのScrolledTextで枠線の色、太さ、スクロールバーの色を変える](https://www.yukiiworks.com/wp-content/uploads/2019/11/python-logo-150x150.png)
![[Laravel]blade上で変数の存在確認](https://www.yukiiworks.com/wp-content/uploads/2019/05/laravel-150x150.png)
![[Android][AdMob]テストでは広告が表示されるのに本番のPlay Storeに公開したら広告が表示されない件について](https://www.yukiiworks.com/wp-content/uploads/2019/04/android-150x150.jpg)

