Wrong decode() from redstone_mapper about observable object in web app

119 views Asked by At

I have different results of using decode() from redstone.dart with object which has observable fields in console and web applications.

My console app has pubspec.yaml

name: 'redstone_decode'
version: 0.0.1
description: console app
environment:
  sdk: '>=1.0.0 <2.0.0'
dependencies:
  redstone: any
  redstone_mapper: any
  observe: any
transformers:
- redstone_mapper # http://redstonedart.org/doc/redstone_mapper.html
- observe:
    entry_points: bin/main.dart

main.dart

import 'dart:convert' show JSON;
import 'package:redstone_mapper/mapper.dart';
import 'package:redstone_mapper/mapper_factory.dart';
import 'package:observe/observe.dart';

class User extends Object with Observable {
  @Field()
    @observable
      String username;
  @observable
    @Field()
      String password;
}

void main() {
  bootstrapMapper();
  User user = new User()
    ..username = "user"
    ..password = "pass";
  String userJson = JSON.encode(encode(user));
  User user1 = decode({
    "username":"user",
    "password":"pass",
    "ERROR":"error"}, User);
  String userJson1 = JSON.encode(encode(user1));
  print(' after decode() '+ userJson1);
}

And it works right. Result in console is:

after decode() {"username":"user","password":"pass"}

I made web app from template "An absolute bare-bones web app". It has pubspec.yaml

name: 'redstone_decode'
version: 0.0.1
description: An absolute bare-bones web app.
environment:
  sdk: '>=1.0.0 <2.0.0'
dependencies:
  browser: '>=0.10.0 <0.11.0'
  redstone: any
  redstone_mapper: any
  observe: any
transformers:
#- redstone_mapper http://redstonedart.org/doc/redstone_mapper.html
- observe:
    entry_points: web/index.html
    entry_points: web/main.dart

main.dart

import "dart:html";
import 'dart:convert' show JSON;
import 'package:redstone_mapper/mapper.dart';
import 'package:redstone_mapper/mapper_factory.dart';
import 'package:observe/observe.dart';

class User extends Object with Observable {
  @Field()
    @observable
      String username;
  @observable
    @Field()
  String password;
}

void main() {
  bootstrapMapper();
  User user = new User()
    ..username = "user"
    ..password = "pass";
  String userJson = JSON.encode(encode(user));
  User user1 = decode({
    "username":"user",
    "password":"pass",
    "ERROR":"error"}, User);
  String userJson1 = JSON.encode(encode(user1));
  querySelector('#output').text = ' after decode() '+ userJson1;
}

Result in Dartium

after decode() {}

Any ideas?

1

There are 1 answers

0
Ticore Shih On

The observe transformer will rewirte field name to __$FieldName, simple field member will become setter/getter. Only getter has keep @Field() annotation..., so decode function will fail.

Workaround, create base class to keep original field name:

import 'package:redstone_mapper/mapper.dart';
import 'package:redstone_mapper/mapper_factory.dart';
import 'package:observe/observe.dart';

class BaseUser {
  @Field()
  String username;
  @Field()
  String password;
}

class User extends BaseUser implements Observable {
  @Field()
  @observable
  String username;
  @Field()
  @observable
  String password;
  toString() => 'User("$username", "$password");';
}

void main() {
  bootstrapMapper();
  User user = new User()
    ..username = "user"
    ..password = "pass";
  String userJson = encodeJson(user);
  print('$userJson');

  User userDes1 = decodeJson('{"username":"user","password":"pass"}', User);
  User userDes2 = decode({"username":"user","password":"pass"}, User);

  print(userDes1); // User("user", "pass");
  print(userDes2); // User("user", "pass");
}