charset_converter 1.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 91

Charset converter #

pub package Tests status

Encode and decode charsets using platform built-in converter. This saves app package size as you don't need any external charset maps or whole libraries like iconv. This package doesn't even contain any Dart dependencies. However this comes with the dependency on the platform.

This package was originally made to help deal with this StackOverflow question and Dart's lack of support for many specific charset like GBK, Big5, Windows-125X or ISO-8859-XX.

Usage #

Checkout these snippets or full example.

Encoding

Uint8List encoded = await CharsetConverter.encode("windows1250", "Polish has óśćł");

Decoding

String decoded = await CharsetConverter.decode("windows1250",
    Uint8List.fromList([0x43, 0x7A, 0x65, 0x9C, 0xE6])); // Hello (Cześć) in Polish

Getting list of available charsets

Helpful if you don't know exact name of the charset.

List<String> charsets = await CharsetConverter.availableCharsets();

Warning: Please note that even if charset is not present on the list, it still might work. This is because the function is not returning the full list of aliases, this is presented in the example of iOS - TIS620 does not appear on the list, but ISO 8859-11, which is actually an alias of TIS620 does. To check if charset is available use CharsetConverter.checkAvailability.

Please also note that names are platform specific and may be different.

Checking availability of charset

bool isAvailable = await CharsetConverter.checkAvailability("EUC-KR");

Preview of what encoding you may find #

This can be helpful if you are not sure what you are looking for.

What's coming #

  • Stream API

How does it work? #

Android comes with Java runtime, which has a built-in Charset class which has convenient encode and decode methods. All it's left to do is use channels to pass the data.

iOS can also work with charsets with CoreFoundation CFString functions fe. CFStringConvertIANACharSetNameToEncoding. We can also easily encode and decode Strings with init and data. It is a little bit cumbersome since we have to convert the values and use ported C API.

Contributing #

Feel free to create issues, fix bugs, add new functionalities even if you are not sure about. Everyone is welcome!

1.0.3 #

  • Update pubspec.yaml to mark supported platforms
  • Add integration tests
  • Add GitHub Actions workflow to run unit and integration tests

1.0.2 #

  • Small improvements in documentation

1.0.1 #

  • Fix links in README files

1.0.0 #

  • Stable release
  • Update example README

0.0.3 #

  • Single charset availability check

0.0.2 #

  • iOS support

0.0.1 #

  • Encoding and decoding
  • Getting list of available charsets
  • Android support
  • Basic documentation

example/lib/main.dart

import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:charset_converter/charset_converter.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _decoded;
  Uint8List _encoded;
  bool _errored = false;

  @override
  void initState() {
    super.initState();
    decodeAndEncode();
  }

  Future<void> decodeAndEncode() async {
    String decoded;
    Uint8List encoded;

    try {
      encoded =
          await CharsetConverter.encode("TIS620", "สวัสดี"); // Hello in Thai
      decoded = await CharsetConverter.decode(
          "windows1250",
          Uint8List.fromList(
              [0x43, 0x7A, 0x65, 0x9C, 0xE6])); // Hello in Polish
    } on PlatformException {
      setState(() {
        _errored = true;
      });
      return;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _decoded = decoded;
      _encoded = encoded;
    });
  }

  /// Generates widget builder for [FutureBuilder] for given [charset]
  Function _charsetAvailabilityView(String charset) {
    return (context, snapshot) {
      if (snapshot.hasData) {
        bool isAvailable = snapshot.data as bool;

        return Text("Is $charset available: " + isAvailable.toString(),
            key: Key('charset_$charset'));
      }
      return CircularProgressIndicator();
    };
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('CharsetConverter example'),
        ),
        body: Center(
            child: Column(children: [
          if (_decoded != null)
            Text("Decoded from Windows CP1250: " + _decoded,
                key: Key('decoded')),
          if (_encoded != null)
            Text("Encoded to TIS620: " + _encoded.join(", "),
                key: Key('encoded')),
          if (_errored) Text("Something went wrong :C"),
          FutureBuilder(
              future: CharsetConverter.checkAvailability(
                  "i_should_not_be_available"),
              builder: _charsetAvailabilityView("i_should_not_be_available")),
          FutureBuilder(
              future: CharsetConverter.checkAvailability("EUC-KR"),
              builder: _charsetAvailabilityView("EUC-KR")),
          Expanded(
              child: FutureBuilder(
            future: CharsetConverter.availableCharsets(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                final charsets = snapshot.data as List<String>;

                return ListView.builder(
                    itemCount: charsets.length,
                    itemBuilder: (context, index) => Text(charsets[index]));
              }
              if (snapshot.hasError) {
                return Text(snapshot.error.toString());
              }
              return CircularProgressIndicator();
            },
          )),
        ])),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  charset_converter: ^1.0.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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:charset_converter/charset_converter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
82
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
91
Learn more about scoring.

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

  • Dart: 2.8.1
  • pana: 0.13.8-dev
  • Flutter: 1.17.0

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test