0

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)

Tomas Ward
  • 854
  • 6
  • 23
  • 1
    Did you see https://stackoverflow.com/questions/37368917/database-schema-update-in-production, https://stackoverflow.com/questions/30168665/is-there-a-way-to-version-objects-in-firebase-that-are-meant-to-be-read-only and https://stackoverflow.com/questions/40349274/changing-firebase-data-model-while-multiple-app-versions-are-in-production? – Frank van Puffelen Jul 31 '22 at 23:13
  • @FrankvanPuffelen basically you need to handle both the legacy and new systems or create a function updateTodoSchema() (in new app version) that grabs old data tree and converts to new? This way all users are in new schema? – Tomas Ward Jul 31 '22 at 23:43
  • Yup, those are pretty much the option: keep the data in a format that all clients can handle, or block older clients from access. You can mix the two of course, to determine the oldest clients you want to support. – Frank van Puffelen Aug 01 '22 at 00:17

0 Answers0