Home > other >  How to make a data model change in a Flutter app in production?
How to make a data model change in a Flutter app in production?

Time:05-19

I am developing my first mobile app with Flutter and I have a doubt.

Suppose the app receives a JSON like this:

{
  "_id": "123",
  "name": "X",
}

To receive it and send it the following model would be created:

class User {
   String id;
   String name;

   User({
      this.id,
      this.name
   });

   factory User.fromJson(Map<String, dynamic> json) => User(
      id: json["_id"],
      name: json["name"]
   );

   Map<String, dynamic> toJson() => {
      "_id": id,
      "name": name
   };
}

Now for some reason I need to change the model structure of that data to, for example:

{
   "id": "123",
   "info": {
      "name":"X",
      "age":"20"
   }
}

In the database there are objects stored with the old structure and, when the app receives them, there will be an error because the new model does not match the old data.

If the app is already in production, what is the most common way to avoid this error without affecting the users?

CodePudding user response:

There are countless versioning ideas and frameworks out there, but the basics are:

  • Update your data to the new model
  • Create a second end point that delivers the new data model to your app
  • Keep the old endpoint, reading from the new data structure, so old apps will still work!
  • Create an app update where you talk to the new endpoint instead and have the new data structure available
  • Drop the old end point when you determine that enough users have made the update (might be "never")

OR:

  • Change the backend
  • Change the frontend
  • Deploy them at the same time and force users to update

The first is more user friendly and easier to handle, since you can work and deploy asynchronously. You can for example deploy the new backend first, while waiting for app store approval of the new app.

CodePudding user response:

In my opinion, you can check if the user is saved with the old structure, if so, you can read it and replace it with the new structure, else, read it normally.

  • Related