How Do I Resolve iOS Failure to Decode UTF-8 on Flutter?

Asked 1 years ago, Updated 1 years ago, 71 views

There is a problem that the HTTP response (bodybyte) cannot be decoded in UTF-8 within the iOS app using Flutter, so I would appreciate it if you could teach me.
The Pixel 2 emulator on Android was able to decode it without any problems, so I think it is an iOS-specific problem.

This is the result of executing the following code on the phone 12 Pro Max emulator on iOS Deployment Target= 9.0.
The problem is that the decoded_body_byte is null.

import'dart:async';
import 'dart: typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import'package: html/dom.dart'as dom;
import 'package: http/http.dart' as http;
import 'package: charset_converter/charset_converter.dart';
import 'package:flutter_user_agent/flutter_user_agent.dart';
// skip

String userAgent;
try{
  userAgent=waitFlutterUserAgent.getPropertyAsync('userAgent');
  print("userAgent:${userAgent}";
} on PlatformException {
  userAgent='<error>';
}
var response = wait http.Client().get(Uri.parse("http://news4vip.livedoor.biz/archives/52385788.html"), headers: {'User-Agent':userAgent});
print("Response status:${response.statusCode}";
print("response.headers:${response.headers['content-type']}");
String decoded_body_byte = wait CharsetConverter.decode("UTF-8", response.bodyBytes);
print("decoded_body_byte:${decoded_body_byte}");// The problem is that the result here is null.
Uint8Listencoded=wait CharsetConverter.encode("UTF-8", "[Image] Chunichi "Cool" Uni Announcement of this season's uniform www");
print("encoded.length:${encoded.length}";
String decoded_body_byte_only_title=wait CharsetConverter.decode("UTF-8", response.bodyBytes.sublist(71,71+78));
print("decoded_body_byte_only_title:${decoded_body_byte_only_title}");

Below is the output of the above code.

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:[Image] Chunichi "Cool" this season's uniform announcement www

The following is the output of the flutter doctor.I look forward to hearing from you.

%flutter doctor
Doctor summary (to see all details, runflutter doctor-v):
[ ]] Flutter (Channel stable, 1.22.5, on macOS 11.120C69darwin-x64, locale ja-JP)

[!] Android toolchain-develop for Android devices (Android SDK version 29.0.2)
    ! Some Android licenses not accepted.Toresolve this, run:flutter doctor --android-licenses
[ ]] Xcode-develop for iOS and macOS (Xcode 12.3)
[!] Android Studio (version 4.1)
    Fl Flutter plugin not installed; this addds Flutter specific functionality.
    D Dart plug in not installed; this adds Dart specific functionality.
[ ]] VS Code (version 1.52.1)
[ ]] Connected device (2 available)

! Doctor found issues in 2 categories.

ios flutter

2022-09-30 14:21

1 Answers

Thank you all for your comments.@Ooper's guess below seems to have been correct.

UTF-8 contains incorrect byte strings

CharsetConverter seemed to be just diverting Swift's NSSstring class.

https://github.com/pr0gramista/charset_converter/blob/master/ios/Classes/SwiftCharsetConverterPlugin.swift#L49

So when I changed the decoder to Plugin below, I was able to decode it as expected.

https://api.flutter.dev/dart-convert/Utf8Codec/decode.html

String decoded=Utf8Decoder(allowMalformed:true).convert(response.bodyBytes);

<allowMalformed: true seems to replace the UTF8 malformed characters.

It was very helpful, thank you.


2022-09-30 14:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.