Search code examples
flutterdartbyteconverterszero

Strange decoding bug in Dart/Flutter with 'dart:convert'


Since I updated Flutter and all libraries, I encounter a strange bug when decoding a list of bytes.

The app communicates with a bluetooth device with flutter_blue library like that:

import 'dart:convert';

var result = await characteristic.read(); // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
return utf8.decode(result, allowMalformed: true);

The decoded string is displayed in a widget. Previously, I had no problem, the string seems empty. But recently everything was updated, the string looks empty in the console but not in the widget since I see several empty squares as character. And the length of the string, even after the trim method, is 15, not 0.

I don't find any reason about this change on internet neither how to solve the problem.

Have you ever met this bug? Do you have a good solution?

Thanks

Edit: The result is the same with allowMalformed = true, of with

new String.fromCharCodes(result)

I think there is a bug with flutter when decoding only 0


Solution

  • As editing in my question, it seems there is a problem with trailing zero.

    From decimal to string, space equal to 32, and 0 is normally a empty character. But it's not anymore.

    The solution I found is

    var result = await characteristic.read();
    var tempResult = List<int>.from(result);
    tempResult.removeWhere((item) => item == 0);
    return utf8.decode(tempResult, allowMalformed: true);
    

    I copy the first list into a mutable list, and remove all 0 from it. That's work perfertly.