And*_*_id 5 gzip string-decoding dart flutter
Json 响应具有 gzip 编码字符串。
var dataList = [
{"Data": "compressedata"},
{"Data": "compressedData"}
];
Run Code Online (Sandbox Code Playgroud)
我尝试了很多方法来解压字符串,但无法得到预期的结果。最后尝试的方法是
List<int> res = base64.decode(base64.normalize(zipText));
print(utf8.decode(res));
Run Code Online (Sandbox Code Playgroud)
其中 zipText 是来自 json 的字符串,这会引发错误
Unhandled Exception: FormatException: Unexpected extension byte (at offset 5)
Run Code Online (Sandbox Code Playgroud)
其他方式
Uint8List compressed = base64.decode(zipText);
var gzipBytes = new GZipDecoder().decodeBytes(compressed);
print(gzipBytes);
Run Code Online (Sandbox Code Playgroud)
抛出错误
Unhandled Exception: FormatException: Invalid GZip Signature flutter
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助。
根据抛出的错误,问题似乎来自您尝试解码的字符串。json 响应中的数据是否已正确解析为字符串?您可能需要考虑验证 json 响应中的“compressedData”是否有效并且可以使用 gzip 进行解码。
如果 on 的值zipText 确实从 - 访问数据,dataList则为 List<Map<String, String>>。确保您能够通过以下方式访问“compressedData”var zipText = '${_dataList[index]['Data']}';
除此之外,您还需要将值从GZipDecoder().decodeBytes(Uint8List)字符串解码。
decode(String zipText) {
// if the sample data is a List<Map<String, String>>
// you need to use the key to access
// the compressed data from the List item
// zipText = '${_dataList[0]['Data']}';
Uint8List compressed = base64.decode(zipText);
var gzipBytes = new GZipDecoder().decodeBytes(compressed);
// Decode List<int> to String
var stringDecoded = utf8.decode(gzipBytes);
debugPrint('decoded: $stringDecoded');
setState(() {
_textEditingControllerEncode.text = stringDecoded;
});
}
Run Code Online (Sandbox Code Playgroud)
这是一个可以玩的简单沙箱。此示例中的方法decode(String)使用您提供的代码片段。
import 'dart:convert';
import 'dart:typed_data';
import 'package:archive/archive.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _formEncodeKey = GlobalKey<FormState>();
final _formDecodeKey = GlobalKey<FormState>();
final _textEditingControllerEncode = TextEditingController();
final _textEditingControllerDecode = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: [
Expanded(
child: Form(
key: _formEncodeKey,
child: Column(
children: <Widget>[
TextFormField(
controller: _textEditingControllerEncode,
decoration: InputDecoration(
hintText: 'Enter some text to Encode'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text to Encode';
}
return null;
},
),
ElevatedButton(
onPressed: () {
if (_formEncodeKey.currentState!.validate()) {
encode(_textEditingControllerEncode.value.text);
}
},
child: Text('Encode'),
),
],
),
),
),
Expanded(
child: Form(
key: _formDecodeKey,
child: Column(
children: <Widget>[
TextFormField(
controller: _textEditingControllerDecode,
decoration: InputDecoration(
hintText: 'Enter some text to Decode'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text to Decode';
}
return null;
},
),
ElevatedButton(
onPressed: () {
if (_formDecodeKey.currentState!.validate()) {
decode(_textEditingControllerDecode.value.text);
}
},
child: Text('Decode'),
),
],
),
),
),
],
),
),
);
}
var _dataList = [
{"Data": "H4sIABqe5GAA//NIzcnJVyjPL8pJUQQAlRmFGwwAAAA="}, // Hello world!
{"Data": "H4sIABuk5GAA/3PLKS0pSS1SKMpPzi5WBADZNee+DgAAAA=="} // Flutter rocks!
];
encode(String zipText) {
var stringBytes = utf8.encode(zipText);
var gzipBytes = GZipEncoder().encode(stringBytes);
var stringEncoded = base64.encode(gzipBytes!);
debugPrint('encoded: $stringEncoded');
setState(() {
_textEditingControllerEncode.clear();
_textEditingControllerDecode.text = stringEncoded;
});
}
decode(String zipText) {
// if the sample data is a List<Map<String, String>>
// you need to use the key to access
// the compressed data from the List item
// zipText = '${_dataList[0]['Data']}';
Uint8List compressed = base64.decode(zipText);
var gzipBytes = new GZipDecoder().decodeBytes(compressed);
// Decode List<int> to String
var stringDecoded = utf8.decode(gzipBytes);
debugPrint('decoded: $stringDecoded');
setState(() {
_textEditingControllerEncode.text = stringDecoded;
_textEditingControllerDecode.clear();
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25659 次 |
| 最近记录: |