I’m encountering a 422 error when attempting to update records in my Airtable database using Flutter. Interestingly, I can fetch and delete records without any issues, but the update operation fails every time.
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.apiToken}"},
);
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'],
profilePic: record['fields']['profilePic'][0]['url'],
status: record['fields']['status'],
email: record['fields']['email'],
city: record['fields']['city'],
phone: record['fields']['phone'],
rating: record['fields']['rating'],
resume: record['fields']['resume'][0]['url'],
latitude: record['fields']['latitude'].toDouble(),
longitude: record['fields']['longitude'].toDouble(),
),
)
});
return employees;
} else {
throw "Failed to load data";
}
}
Future<String> removeEmployee(String recordId) async {
final response = await http.delete(
Uri.https("api.airtable.com", "/v0/${AppConfig.baseId}/employees/$recordId"),
headers: {"Authorization": "Bearer ${AppConfig.apiToken}"},
);
if (response.statusCode == 200) {
return recordId;
} else {
throw Exception(response.statusCode);
}
}
Future<Employee> modifyEmployee(String recordId, String newRating) async {
final response = await http.patch(
Uri.https("api.airtable.com", "/v0/${AppConfig.baseId}/employees/$recordId"),
headers: {
"Authorization": "Bearer ${AppConfig.apiToken}",
"Content-Type": "application/json",
},
body: jsonEncode({
"fields": {
"rating": newRating,
}
}),
);
if (response.statusCode == 200) {
return Employee.fromJson(jsonDecode(response.body));
} else {
throw Exception(response.statusCode);
}
}
}
employee_model.dart
class Employee {
String recordId;
String firstName;
String lastName;
String profilePic;
String status;
String phone;
String email;
String city;
String resume;
String rating;
double latitude;
double longitude;
Employee({
required this.recordId,
required this.firstName,
required this.lastName,
required this.profilePic,
required this.status,
required this.phone,
required this.email,
required this.city,
required this.resume,
required this.latitude,
required this.longitude,
required this.rating,
});
factory Employee.fromJson(Map<String, dynamic> json) {
return Employee(
recordId: json['id'],
firstName: json['fields']['firstName'],
lastName: json['fields']['lastName'],
profilePic: json['fields']['profilePic'][0]['url'],
status: json['fields']['status'],
email: json['fields']['email'],
city: json['fields']['city'],
phone: json['fields']['phone'],
rating: json['fields']['rating'],
resume: json['fields']['resume'][0]['url'],
latitude: json['fields']['latitude'].toDouble(),
longitude: json['fields']['longitude'].toDouble(),
);
}
}
I’m really stuck on this one. Any help would be amazing!