updateRecord method

Future<void> updateRecord(
  1. String tableName,
  2. AirtableRecord record
)

Updates an existing record in the specified tableName.

  • record: An instance of AirtableRecord containing the updated fields.

Throws an AirtableException if the request fails.

Implementation

Future<void> updateRecord(String tableName, AirtableRecord record) async {
  // Ensure the fields are nested within 'fields' and include 'typecast': true
  final Map<String, dynamic> body = {
    'fields': record.fields,
    'typecast': true,
  };

  final response = await http.patch(
    Uri.parse('$_endpoint/$baseId/$tableName/${record.id}'),
    headers: {
      'Authorization': 'Bearer $apiKey',
      'Content-Type': 'application/json',
    },
    body: jsonEncode(body), // Use the nested 'fields' format
  );

  if (response.statusCode != 200) {
    final errorBody = jsonDecode(response.body);
    throw AirtableException(
      message: 'Failed to update record',
      details: errorBody['error']?['message'],
    );
  }
}