flutter/dart: 如何在 flutter 中 decompress/inflate zlib 二进制字符串

flutter/dart: How to decompress/inflate zlib binary string in flutter

我在我的 nodejs 服务器上使用 pako 包,并将压缩的二进制字符串从服务器发送到我的 flutter 客户端。我无法在 flutter 客户端上 decompress/inflate 它。我尝试了库的组合

我的服务器 NodeJS 代码:

    var pako = require('pako');
    let buffer = pako.deflate(JSON.stringify(userModels), { to: 'string' });
    //Server code for sending to client

我的 Flutter 代码:

import 'dart:io';
import 'dart:convert';
List<int> gzipBytes = serverResponse.data.codeUnits;
List<int> stringBytes = gzip.decode(gzipBytes);
var data = utf8.decode(stringBytes);

我收到以下异常..

Restarted application in 1,725ms. E/flutter (25340): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: InternalError: 'Filter error, bad data' E/flutter (25340): #0
_FilterImpl.processed (dart:io-patch/filter_patch.dart:11:32) E/flutter (25340): #1 _FilterSink.addSlice (dart:io/data_transformer.dart:610:29) E/flutter (25340): #2
_FilterSink.add (dart:io/data_transformer.dart:596:5) E/flutter (25340): #3 ZLibDecoder.convert (dart:io/data_transformer.dart:465:9) E/flutter (25340): #4
Codec.decode (dart:convert/codec.dart:26:34)

你没有显示你是如何接收数据的,但也不要通过字符串。例如,如果使用 package:http,请使用:

var bytes = response.bodyBytes;

接下来,您似乎不应该使用 gzip,而应该使用 zlib

var inflated = zlib.decode(bytes);
var data = utf8.decode(inflated);
json.decode(data);