createRecord method

Future<AirtableRecord> createRecord(
  1. String tableName,
  2. Map<String, dynamic> data
)

Creates a new record in the specified tableName.

  • data: A map containing the field names and values for the new record.

Returns a Future that resolves to the created AirtableRecord.

Throws an AirtableException if the request fails.

Implementation

Future<AirtableRecord> createRecord(
    String tableName, Map<String, dynamic> data) async {
  final recordWithoutId = {'fields': data, 'typecast': true};

  final response = await http.post(
    Uri.parse('$_endpoint/$baseId/$tableName'),
    headers: {
      'Authorization': 'Bearer $apiKey',
      'Content-Type': 'application/json',
    },
    body: jsonEncode(recordWithoutId),
  );

  if (response.statusCode == 200 || response.statusCode == 201) {
    return AirtableRecord.fromJson(jsonDecode(response.body));
  } else {
    final errorBody = jsonDecode(response.body);
    throw AirtableException(
      message: 'Failed to create record',
      details: errorBody['error']?['message'],
    );
  }
}