How to use the @MirrorUsed annotation on Serialization lib (dart2js)?


I using the serialization library ( to persiste and recover complex objects.

An excellent library to convert complex objects (also converts to JSON!). Congratulations to Google people!

However, necessary to transform dart in js (dart2js) and I’m not succeeding in using MirrorUsed. I would like to reduce the code size!

When I use @MirrorUsed like following, serialization does not work more.


@MirrorsUsed(targets: 'serialization.serialization_mirrors.Serialization', override: '*')
import 'dart:mirrors';    
import 'package:serialization/serialization_mirrors.dart';
// [..other imports..]

class MainApp extends PolymerElement {
  var serializationProdutos;
  List produtos = new List();

  void ready() {

    Produto p = new Produto()..descricao = "SUPER TESTE"..unidade = "Unitário";
    Produto p2 = new Produto()..descricao = "SUPER TESTE 2"
    ..ehComposto = true

    Map m = new Map()
    ..putIfAbsent("expandir", () => true)..putIfAbsent(
      "produto", () => p);
    Map mm = new Map()
    ..putIfAbsent("expandir", () => true)..putIfAbsent(
      "produto", () => p2);

    (mm['produto'] as Produto).componentes.add(new ProdutoComponente()..produtoComponente = p);
    add('produtos', m);
    add('produtos', mm);

  salvarDados(Event e, [_]) {
    serializationProdutos = new Serialization();
    var jsonProdutos = JSON.encode(serializationProdutos.write(produtos));  
    window.localStorage['precoMi_produtos'] = jsonProdutos;

  carregarDados(Event e, [_]) {
     var jsonProdutos = window.localStorage['precoMi_produtos'];
    if (jsonProdutos != null) {
      try {
           serializationProdutos = new Serialization();
           List pro =;
           addAll('produtos', pro);
      } catch (e) {
            'Error local storage. ${e}');

Am I using @MirrorUsed correctly?


If you’re using Polymer, then it’s using the Reflectable package, which is much easier to optimize than MirrorsUsed, and I’m not even sure that the two will work nicely together. Really, the Serialization package needs to be updated to enable this.

As a short-term workaround, what I’d suggest is using non-mirrored serialization rules. You can write these by hand, which is more work for you in terms of maintenance, but if the number of classes is not too large, it’s probably acceptable, and it should produce quite small code without any extra effort on your part. Or, if your classes are relatively simple (no complex constructors, ordering dependencies) you can use the serialization transformer to generate them. Unfortunately there’s a bug where dartdocs is not showing the library comments, so you’d have to look at the comments on e.g. and

Answered By – Alan Knight

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *