saveDatabaseModel function

Future<List<String>?> saveDatabaseModel(
  1. Map<String, dynamic> context
)

This function runs in an isolated thread. Please use Database.saveModel

Implementation

Future<List<String>?> saveDatabaseModel(Map<String, dynamic> context) async {
  try {
    List<Map<String, dynamic>> models = context['models'];

    bool exists = false;

    for (int i = 0; i < models.length; i++) {
      if (models[i]['id'] == context['model'].getId()) {
        models[i] = context['model'].toArray();

        exists = true;
        break;
      }
    }

    if (!exists) {
      models.add(context['model'].toArray());
    }

    List<String> data = [];

    for (int i = 0; i < models.length; i++) {
      data.add(jsonEncode(models[i]));
    }

    return data;
  } catch (e) {
    print(e);
  }

  return null;
}