FlutterでiOSアプリを作成している過程でHTTPのレスポンスをUTF-8でデコードできない問題が発生しており、解決したく思います。
AndroidのPixel 2のエミュレータでは問題なくデコードできていたのでiOS特有の問題であると考えています。
以下いPhone12 Pro Maxのエミュレータを使い、実行した結果になります。decoded_body_byteがnullになってしまうのが問題です。
dart
1import 'dart:async'; 2import 'dart:typed_data'; 3import 'package:flutter/material.dart'; 4import 'package:flutter/services.dart'; 5import 'package:html/dom.dart' as dom; 6import 'package:http/http.dart' as http; 7import 'package:charset_converter/charset_converter.dart'; 8import 'package:flutter_user_agent/flutter_user_agent.dart'; 9// skip 10 11String userAgent; 12try { 13 userAgent = await FlutterUserAgent.getPropertyAsync('userAgent'); 14 print("userAgent: ${userAgent}"); 15} on PlatformException { 16 userAgent = '<error>'; 17} 18var response = await http.Client().get(Uri.parse("http://news4vip.livedoor.biz/archives/52385788.html"), headers: {'User-Agent': userAgent}); 19print("Response status: ${response.statusCode}"); 20print("response.headers: ${response.headers['content-type']}"); 21String decoded_body_byte = await CharsetConverter.decode("UTF-8", response.bodyBytes); 22print("decoded_body_byte: ${decoded_body_byte}"); // ここの結果がnullになってしまうことが問題になっています。 23Uint8List encoded = await CharsetConverter.encode("UTF-8", "【画像】中日「かっこいい」今季のユニホーム発表www"); 24print("encoded.length: ${encoded.length}"); 25String decoded_body_byte_only_title = await CharsetConverter.decode("UTF-8", response.bodyBytes.sublist(71, 71 + 78)); 26print("decoded_body_byte_only_title: ${decoded_body_byte_only_title}");
上記の出力結果は以下になります。
2021-01-23 17:09:29.964984+0900 Runner[89036:14458916] flutter: userAgent: CFNetwork/1209 Darwin/20.2.0 (iPhone iOS/14.3) 2021-01-23 17:09:30.187131+0900 Runner[89036:14458916] flutter: Response status: 200 2021-01-23 17:09:30.190547+0900 Runner[89036:14458916] flutter: response.headers: text/html; charset=utf-8 2021-01-23 17:09:30.195755+0900 Runner[89036:14458916] flutter: decoded_body_byte: null 2021-01-23 17:09:30.197368+0900 Runner[89036:14458916] flutter: encoded.length: 78 2021-01-23 17:09:30.198128+0900 Runner[89036:14458916] flutter: decoded_body_byte_only_title: 【画像】中日「かっこいい」今季のユニホーム発表www
以下flutter doctorの出力結果になります。
% flutter doctor Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 1.22.5, on macOS 11.1 20C69 darwin-x64, locale ja-JP) [!] Android toolchain - develop for Android devices (Android SDK version 29.0.2) ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses [✓] Xcode - develop for iOS and macOS (Xcode 12.3) [!] Android Studio (version 4.1) ✗ Flutter plugin not installed; this adds Flutter specific functionality. ✗ Dart plugin not installed; this adds Dart specific functionality. [✓] VS Code (version 1.52.1) [✓] Connected device (2 available) ! Doctor found issues in 2 categories.
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/23 23:44
2021/01/24 07:21
2021/01/24 07:48
2021/01/24 08:03
2021/01/24 10:01
2021/01/24 11:07