Flutter Airtable Update Error 422 - Unhandled Exception During Data Modification

I’m facing an error while trying to update records in my Airtable database using Flutter. The error message is [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception: 422. I can retrieve and delete records without any issues, but updating them never seems to work.

Here’s a snippet of my code:

employee_service.dart

import 'package:http/http.dart' as http;
import 'package:myapp/models/employee_model.dart';
import 'package:myapp/config/app_config.dart';

class EmployeeService {
  final Uri employeeUrl = Uri.https(
    "api.airtable.com",
    "/v0/${AppConfig.baseId}/employees",
    {"maxRecords": "100", "view": "Main View"},
  );

  Future<List<Employee>> fetchEmployees() async {
    final response = await http.get(
      employeeUrl,
      headers: {"Authorization": "Bearer ${AppConfig.token}"},
    );

    if (response.statusCode == 200) {
      var jsonData = jsonDecode(response.body);
      var records = jsonData['records'];
      List<Employee> employees = [];
      
      records.forEach(
        (record) => {
          employees.add(
            Employee(
              recordId: record['id'],
              firstName: record['fields']['firstName'],
              lastName: record['fields']['lastName'],
              avatar: record['fields']['avatar'][0]['url'],
              department: record['fields']['department'],
              email: record['fields']['email'],
              city: record['fields']['city'],
              phone: record['fields']['phone'],
              status: record['fields']['status'],
              resume: record['fields']['resume'][0]['url'],
              latitude: record['fields']['latitude'].toDouble(),
              longitude: record['fields']['longitude'].toDouble(),
            ),
          )
        },
      );

      return employees;
    } else {
      throw "Request failed!";
    }
  }

  Future<String> removeEmployee(String recordId) async {
    final result = await http.delete(
      Uri.https(
          "api.airtable.com", "/v0/${AppConfig.baseId}/employees/$recordId"),
      headers: {"Authorization": "Bearer ${AppConfig.token}"},
    );

    if (result.statusCode == 200) {
      return recordId;
    } else {
      throw Exception(result.statusCode);
    }
  }

  Future<Employee> modifyEmployee(String recordId, String newStatus) async {
    final result = await http.patch(
      Uri.https(
          "api.airtable.com", "/v0/${AppConfig.baseId}/employees/$recordId"),
      headers: {
        "Authorization": "Bearer ${AppConfig.token}",
        "Content-Type": "application/json",
      },
      body: jsonEncode({
        "fields": {
          'status': newStatus,
        }
      }),
    );

    if (result.statusCode == 200) {
      return Employee.fromJson(jsonDecode(result.body));
    } else {
      throw Exception(result.statusCode);
    }
  }
}

employee_model.dart

class Employee {
  String recordId;
  String firstName;
  String lastName;
  String avatar;
  String department;
  String phone;
  String email;
  String city;
  String resume;
  String status;
  double latitude;
  double longitude;

  Employee({
    required this.recordId,
    required this.firstName,
    required this.lastName,
    required this.avatar,
    required this.department,
    required this.phone,
    required this.email,
    required this.city,
    required this.resume,
    required this.latitude,
    required this.longitude,
    required this.status,
  });

  factory Employee.fromJson(Map<String, dynamic> data) {
    return Employee(
      recordId: data['id'],
      firstName: data['fields']['firstName'],
      lastName: data['fields']['lastName'],
      avatar: data['fields']['avatar'][0]['url'],
      department: data['fields']['department'],
      email: data['fields']['email'],
      city: data['fields']['city'],
      phone: data['fields']['phone'],
      status: data['fields']['status'],
      resume: data['fields']['resume'][0]['url'],
      latitude: data['fields']['latitude'].toDouble(),
      longitude: data['fields']['longitude'].toDouble(),
    );
  }
}

I’m attempting to call the update method when the users swipe cards, but I’m continuously getting the 422 error. What could be the issue with my implementation?

Thanks in advance for any assistance!

The 422 error typically indicates that Airtable is rejecting your request due to field validation issues. Looking at your code, I suspect the problem might be with the status field itself. In my experience with Airtable APIs, this usually happens when you’re trying to update a field with a value that doesn’t match the field’s configuration in your base. Check your Airtable base to see if the ‘status’ field has any restrictions - it might be a single select field with predefined options, or there could be validation rules that your newStatus value doesn’t meet. Also verify that the field name is exactly ‘status’ (case sensitive) in your Airtable schema. Another possibility is that the field might be computed or linked to another table, making it read-only. Try updating a simple text field first to isolate whether it’s specific to the status field or a broader issue with your update implementation.

had similar issue few weeks ago. check if your airtable field is single-select type - you cant just pass any string value, it has to match exactly whats in the dropdown options. also 422 usually means validation error so maybe try logging the actual response body to see whats failing instead of just the status code.