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?
The observe transformer will rewirte field name to
__$FieldName
, simple field member will become setter/getter. Only getter has keep@Field()
annotation..., sodecode
function will fail.Workaround, create base class to keep original field name: