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!