create method

Future<List<String>> create (
  1. String dbID,
  2. String collectionName,
  3. List<Map<String, dynamic>> values
)

Create a new instance(s) in the collection

class Person {
  final String ID;
  final String firstName;
  final String lastName;
  final int age;
  Person(this.ID, this.firstName, this.lastName, this.age);
  Person.fromJson(Map<String, dynamic> json)
      : ID = json['ID'],
        firstName = json['firstName'],
        lastName = json['lastName'],
        age = json['age'];

  Map<String, dynamic> toJson() =>
    {
      'ID': ID,
      'firstName': firstName,
      'lastName': lastName,
      'age': age
    };
}

final model = Person('', 'Adam', 'Doe', 24);
try {
  final response = await client.create(dbId, 'Person', [model.toJson()]);
  expect(response.length, 1);
  collectionID = response[0];
  expect(true, true);
} catch (error) {
  expect(error.toString(), '');
}

Implementation

Future<List<String>> create(String dbID, String collectionName, List<Map<String, dynamic>> values) async {
  final request = CreateRequest();
  request.dbID = base64.decode(dbID);
  request.collectionName = collectionName;
  for (var i=0; i<values.length; i++) {
    if (!values[i].containsKey('ID')) {
      values[i]['ID'] = _uuid.v4();
    }
    final valString = json.encode(values[i]).toString();
    request.instances.add(utf8.encode(valString));
  }
  final response = await _stub.create(request);
  return response.instanceIDs;
}