Permalink
Browse files

Storage use JSON.stringify

  • Loading branch information...
1 parent 11c2607 commit e07ca141b1cd55bc912647a7b911da644928d5cd @MathieuLorber committed Oct 23, 2012
Showing with 6 additions and 15 deletions.
  1. +6 −11 labs/architecture-examples/dart/dart/TodoApp.dart
  2. +0 −4 labs/architecture-examples/dart/dart/app.dart
@@ -26,8 +26,9 @@ class TodoApp {
String jsonList = window.localStorage["todos-vanilladart"];
if(jsonList != null) {
try {
- List<Map> todos = JSON.parse(jsonList);
- todos.forEach((Map todo) {
+ List<String> todos = JSON.parse(jsonList);
+ todos.forEach((String jsonTodo) {
+ Map todo = JSON.parse(jsonTodo);
addTodo(new Todo(todo['id'], todo['title'], Boolean.parse(todo['completed'])));
});
} catch (e) {
@@ -170,16 +171,10 @@ class TodoApp {
void save() {
StringBuffer storage = new StringBuffer('[');
+ List<Todo> todos = new List<Todo>();
todoElements.forEach((TodoElement todoElement) {
- storage.add(todoElement.todo.toJson());
- storage.add(',');
+ todos.add(todoElement.todo);
});
- String finalJson;
- if(storage.toString() != '[') {
- finalJson = '${storage.toString().substring(0, storage.toString().length - 1)}]';
- } else {
- finalJson = '[]';
- }
- window.localStorage["todos-vanilladart"] = finalJson;
+ window.localStorage["todos-vanilladart"] = JSON.stringify(todos);
}
}
@@ -17,10 +17,6 @@ class Todo {
bool completed;
Todo(String this.id, String this.title, [bool this.completed = false]);
-
- String toJson() {
- return '{"id": "$id", "title": "$title", "completed": "$completed"}';
- }
}
class UUID {

1 comment on commit e07ca14

@MathieuLorber
Owner

Ok, the JsonObject lib is just needed to get an object instead of a Map after parse. I'm still not completely satisfied with this code. I'll try with the lib.

Please sign in to comment.