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!