You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dart中UTF-8解码JSON数据触发FormatException错误的问题求助

Fixing UTF-8 Decoding FormatException in Flutter HTTP Requests

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-Type header, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 03:02:34