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

Ramesh Guntha

I am using pako package on my nodejs server and sending compressed binary string from server to my flutter client. I am unable to decompress/inflate it on the flutter client. I have tried a combination of libraries

My Server NodeJS Code:

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

My Flutter Code:

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

I am getting the following exception..

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)

Richard Heap

You don't show how you are receiving the data, but don't go via a string. For example, if using package:http, use:

var bytes = response.bodyBytes;

Next, it doesn't look like you should be using gzip, but rather zlib;

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related