LoginSignup
2
1

More than 3 years have passed since last update.

Amplify Flutter の API.get(REST)で日本語が文字化け

Last updated at Posted at 2021-04-07

Amplify Flutterの一般提供がはじまったので、CognitoやREST APIなど使って、アプリを作成しています。

公式のサンプル通り記載すると、日本語が文字化けるので、対応をメモしておきます。

  • 変更前

公式のサンプル通りに、String.fromCharCodes(response.data)で文字列に変換すると、日本語が文字化けしてしまいます。

try {
    RestOptions options = RestOptions(
        path: '/todo',
        queryParameters: {
            'q' : 'test'
        }
    );
    RestOperation restOperation = Amplify.API.get(
        restOptions: options
    );
    RestResponse response = await restOperation.response;
    print('GET call succeeded'); 
    print(new String.fromCharCodes(response.data)); // <------- 変更箇所 fromCharCodesで文字列変換すると、日本語が文字化けします。
} on ApiException catch (e) {
    print('GET call failed: $e');
} 
  • 変更後

レスポンスに日本語が含まれている場合は、utf8.decode(response.data)で文字列に変換しましょう。

import 'dart:convert';

...(中略)...

try {
    RestOptions options = RestOptions(
        path: '/todo',
        queryParameters: {
            'q' : 'test'
        }
    );
    RestOperation restOperation = Amplify.API.get(
        restOptions: options
    );
    RestResponse response = await restOperation.response;
    print('GET call succeeded'); 
    print(utf8.decode(response.data)); // <------- RestResponse.dataには、byte配列 (Uint8List) が入っているので、utf8でデコードしましょう。
} on ApiException catch (e) {
    print('GET call failed: $e');
} 

参考

2
1
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
1