flutterdata / flutter_data

Seamlessly manage persistent data in your Flutter apps
MIT License
410 stars 31 forks source link

Batch Create or Update #265

Closed yadferhad closed 7 months ago

yadferhad commented 7 months ago

How can we do batch operations (creating multiple data at the same time, but calling the server just once for saving) with flutter_data? If we use a custom endpoint, how do we handle the local version?

frank06 commented 7 months ago

Here's an example of a custom endpoint, notice that calling super.onSuccess will do the default deserializing/saving handling. It knows how to handle single and multiple models, have a look at it and see if you can pass it data such that it handles multiple. Otherwise you can roll your own.

Future<List<User>> saveUsers(...) async {
    return sendRequest(
      // ...
      onSuccess: (data, label) async {
        // calling super.onSuccess will deserialize & save
        var users = await super.onSuccess(data, label);
        // ...
        return users;
      },
    );
  }