ย ยทย Business Owner

create JSON object from submitted form

Custom Code

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?

What have you tried so far?

I have looked into creating a page state with the values in a datatype field but still not sure how to get this into a JSON object.

Did you check FlutterFlow's Documentation for this topic?
Yes
1