I have a certain database structure that I am using for my data in Firebase Realtime Database:
todo_list:
fetch dog: true
feed him: false
I want to change it to this:
todo_list:
randomId1
title: fetch dog
checked: true
randomId2
title: feed him
checked: false
What steps should I take to avoid data errors, and let users continue using the app even if they have the old data structure?
Would I have to differentiate functions like this?
static List<ToDo> todoFromJson(Map todoMap) {
List<ToDo> todoList = [];
try {
//new schema
todoMap.forEach((key, value) {
String id = key;
String title = value['title'];
bool checked = value['checked'];
todoList.add(ToDo(title: title, checkboxBool: checked, id: id));
});
} catch (e) {
//old schema
todoMap.forEach((key, value) {
String title = key;
bool checked = value;
todoList.add(ToDo(title: title, checkboxBool: checked, id: ''));
});
}
return todoList;
}
Note: the reason behind these change is because if a user writes a forward slash /
in the title, the database creates a child note and my app doesn't load the data (Using Provider package)