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

Asked 5 months ago, Updated 5 months ago, 16 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: 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;
} on PlatformException {
var response = wait http.Client().get(Uri.parse(""), headers: {'User-Agent':userAgent});
print("Response status:${response.statusCode}";
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");
String decoded_body_byte_only_title=wait CharsetConverter.decode("UTF-8", response.bodyBytes.sublist(71,71+78));

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 [email protected]'s guess below seems to have been correct.

UTF-8 contains incorrect byte strings

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

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

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

© 2023 OneMinuteCode. All rights reserved.