I am using pocketBase with Futterflow and I have created a custom action that handles all record creation. I want to pass in a JSON object that holds the data from the form submitted. It would look something like this:
{
"tableName": "Users",
"fields": {
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"isActive": true
}
}
My custom function will look like this (not tested):
Future<void> createRecord(dynamic recordInfo) async {
final pb = PocketBase('https://*****.pockethost.io');
// Extract tableName from recordInfo.
String tableName = recordInfo['tableName'];
List<dynamic> fields = recordInfo['fields'];
Map<String, dynamic> body = {};
for (var field in fields) {
body[field['name']] = field['value'];
}
try {
// Correctly use the named argument 'body:' as specified in PocketBase docs.
final record = await pb.collection(tableName).create(body: body);
print("Record created successfully!");
} catch (e) {
print("Error creating record: $e");
}
}
My question is, how do I create a JSON object to pass to the function when the user submits the form? Is there an easy way to do this?