Dart中UTF-8解码JSON数据触发FormatException错误的问题求助
Hey there, let's work through that "Missing extension byte" error you're hitting when decoding your HTTP response. Here's a breakdown of what's likely going wrong and how to fix it:
Why This Error Happens
That FormatException pops up because the raw bytes in response.bodyBytes don’t follow proper UTF-8 encoding rules. Common causes include:
- The server is actually sending content in a different encoding (like GBK or ISO-8859-1) instead of UTF-8.
- The HTTP client automatically decoded the response using a charset specified in the
Content-Typeheader, so forcing UTF-8 decoding on the raw bytes creates conflicts. - There are corrupted or invalid bytes in the response stream.
Solution 1: Respect the Server's Declared Encoding
First, check what encoding the server specifies in its response headers, then use that to decode the bytes instead of forcing UTF-8:
import 'package:http/http.dart' as http; import 'dart:convert'; // Add this dependency to pubspec.yaml if you need non-UTF-8 support: // charset_converter: ^2.0.0 import 'package:charset_converter/charset_converter.dart'; Future<Products> getProduct() async { var response = await http.get(Uri.parse(url)); // Extract charset from the Content-Type header String? charset; final contentType = response.headers['content-type']; if (contentType != null && contentType.contains('charset=')) { charset = contentType.split('charset=').last.trim(); } String decodedBody; if (charset != null && charset.toLowerCase() != 'utf-8') { // Decode using the server-specified charset decodedBody = await CharsetConverter.decode(charset, response.bodyBytes); } else { // Fall back to UTF-8 (with tolerance for minor glitches) decodedBody = utf8.decode(response.bodyBytes, allowMalformed: true); } var decodedJson = json.decode(decodedBody); products = Products.fromJson(decodedJson); return products; }
Solution 2: Force UTF-8 with Malformed Byte Tolerance
If you’re sure the server should send UTF-8 content (and invalid bytes are just small glitches), use Dart’s allowMalformed parameter (available in Dart 2.15+) to skip over invalid bytes instead of throwing an error:
import 'package:http/http.dart' as http; import 'dart:convert'; Future<Products> getProduct() async { var response = await http.get(Uri.parse(url)); // Replace invalid UTF-8 bytes with the Unicode replacement character var decodedBody = utf8.decode(response.bodyBytes, allowMalformed: true); var decodedJson = json.decode(decodedBody); products = Products.fromJson(decodedJson); return products; }
Solution 3: Ask the Server for UTF-8 Explicitly
If the server’s Content-Type header doesn’t specify charset=utf-8, explicitly request UTF-8 content by adding an Accept-Charset header to your request:
var response = await http.get( Uri.parse(url), headers: {'Accept-Charset': 'utf-8'}, );
内容的提问来源于stack exchange,提问作者Bora Tan Demir

