Flutter Airtable Error: [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: 422

I keep encountering the error [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: 422 while attempting to modify records in my Airtable Database. The operations for retrieving and deleting data work fine, but I am unable to perform updates. Below is the relevant code for my implementation.

import 'package:http/http.dart' as http;
import 'package:tinjob/model/custom_data_model.dart';

class DatabaseService {
  final Uri apiUrl = Uri.https(
    "api.airtable.com",
    "/v0/${Config.databaseBase}/records",
    {"maxRecords": "500", "view": "Grid view"},
  );

  Future<List<CustomDataModel>> fetchRecords() async {
    final response = await http.get(
      apiUrl,
      headers: {"Authorization": "Bearer ${Config.apiKey}"},
    );

    if (response.statusCode == 200) {
      var jsonData = jsonDecode(response.body);
      var items = jsonData['records'];
      List<CustomDataModel> records = [];
      items.forEach((item) => records.add(CustomDataModel.fromJson(item)));
      return records;
    } else {
      throw Exception('Failed to load data');
    }
  }

  Future<CustomDataModel> modifyRecord(String id, String newValue) async {
    final response = await http.put(
      Uri.https(
        "api.airtable.com",
        "/v0/${Config.databaseBase}/records/$id",
      ),
      headers: {
        "Authorization": "Bearer ${Config.apiKey}",
      },
      body: jsonEncode(<String, String>{'fieldName': newValue}),
    );

    if (response.statusCode == 200) {
      return CustomDataModel.fromJson(jsonDecode(response.body));
    } else {
      throw Exception('Update failed with status: ${response.statusCode}');
    }
  }
}

I’m really hoping someone can assist me in resolving this issue, as I’m currently at a standstill. Thank you!

In my experience with Airtable and APIs, the issue might also stem from missing headers, especially ‘Content-Type’. Try adding ‘Content-Type’: ‘application/json’ in the headers of your ‘modifyRecord’ method. Sometimes the API needs explicit instruction on the content type, even though you’re sending JSON. Also, verify that the record ID you’re using in the URL is correct and exists in your Airtable. Any mismatch there could also result in a 422 error.

The 422 status code typically indicates that the data format is not quite right or some required fields are missing for the update. Make sure you are correctly structuring the JSON body you send in the “modifyRecord” function. Instead of directly using “jsonEncode(<String, String>{‘fieldName’: newValue})”, perhaps ensure the structure matches what Airtable expects. Try wrapping the data with the correct fields in the ‘fields’ object like this:

body: jsonEncode({
  'fields': {'fieldName': newValue},
})

Additionally, double-check that ‘fieldName’ matches exactly with what is in your Airtable schema.

hey! it might be a good idea to check if the api url paths are correctly formed. little typos can make a big diff… :wink: Also, verify that object keys & names match exactly what the api needs. details can sometimes trip us up!