binary_codec 2.0.2

binary_codec #

Dart library for converting standard data types to a binary format and back

Supported datatypes: Null, bool, double, int, Uint8List, String, Map, List. Maps and Lists may contain any of the supported datatypes as keys and values.

On dart2js ints are always encoded as doubles. And if you try to decode ints on dart2js out of the safe range you will probably not get the exact value. But as long as you stay on one platform (not switching between dartvm and dart2js) your on the safe side and don't need to think about it.

1.0.X #

Not safe, don't use them.

2.0.1 #

Forgot the :) Now I've rewritten all the code, so it makes more sense, and does more of the things we expect it to do. Changed the license to MIT since there are some hours invested in this code know.

2.0.2 #

run formatter


import 'dart:math';
import 'dart:typed_data';

import '../lib/binary_codec.dart';

/// Example text
const loremIpsum = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, '
    'sed diam nonumy eirmod tempor invidunt ut labore et dolore '
    'magna aliquyam erat, sed diam voluptua. At vero eos et accusam '
    'et justo duo dolores et ea rebum. Stet clita kasd gubergren, no '
    'sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem '
    'ipsum dolor sit amet, consetetur sadipscing elitr, sed diam '
    'nonumy eirmod tempor invidunt ut labore et dolore magna '
    'aliquyam erat, sed diam voluptua. At vero eos et accusam '
    'et justo duo dolores et ea rebum. Stet clita kasd gubergren, '
    'no sea takimata sanctus est Lorem ipsum dolor sit amet.';

void main() {
  var exampleObject = generateSomeData();

  /// Encode the generated data
  Uint8List encoded = binaryCodec.encode(exampleObject);

  /// Decode the encoded data
  var decoded = binaryCodec.decode(encoded);

  /// Check if they are the same
  if (exampleObject.toString() == decoded.toString()) {
    /// They should be the same
  } else {
    /// Else theres a bug in the library
    /// Please send the [exampleObject] that leads here and on which 
    /// platform you are to the author of this library (
    print('Wait... What???');

dynamic generateSomeData() {
  var random = Random();
  var shortBinaryLists = List.generate(16, (length) {
    return Uint8List.fromList(
      List.generate(length, (_) => random.nextInt(256)),
  var someDoubles = [
  var someInts = [321, 43956, -23459, 0, 1, -1, -239458];
  return {
    'small positive ints': List.generate(128, (x) => x),
    'small negative ints': List.generate(64, (x) => -x),
    'short binary lists': shortBinaryLists,
    'short texts': ['hello world', 'lorem ipsum', 'asdf qwer jklö'],
    'ints': someInts,
    'null': null,
    'booleans': [true, false],
    'doubles': someDoubles,
    'bigger binary lists':
        Uint8List.fromList(List.generate(1266, (x) => x % 256)),
    'bigger texts': loremIpsum.substring(0, 430),
    'lists': ['1', 2, 3.0, List.generate(4, (_) => 4), 'Five'],
    'maps': {'key1': 'value1', true: false, false: true, 4.0: 4, 5: 5.0},

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  binary_codec: ^2.0.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:binary_codec/binary_codec.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Feb 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Fix lib/binary_codec.dart. (-0.50 points)

Analysis of lib/binary_codec.dart reported 1 hint:

line 18 col 47: Avoid const keyword.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0