Create Rows
Create a Single Row
You can create a new row in a Data Store table of the given instance using the newRow() method. You must set the values of the rows in the table, by specifying the column name and the corresponding row value as a key-value pair.
The <ROW_INSTANCE> used in the code syntax below is the instance defined in the Row Instance page.
copyFuture<(APIResponse, ZCatalystRow)> <ROW_INSTANCE>.create()
A sample code snippet with creating a row instance is shown below:
copyvar row = ZCatalystApp.getInstance() .getDataStoreInstance() .getTableInstance(identifier: 'Products') .newRow(); //Defining the row instance for the table and passing the key-value data for the row row.setColumnValue('product_name', 'power_bank'); row.setColumnValue('product_price', 2000); row.setColumnValue('product_quantity', 2); try { var (response, row) = await row.create(); print('New row created successfully : ${row.id}'); } on ZCatalystException catch (ex) { print('Failed to create the row: $exception'); }
Create Multiple Rows
Catalyst Flutter SDK enables you to create multiple rows at a time in a table of the given instance. This is done by passing a list containing the rows that need to be inserted in the table. The list is passed as an argument to the createRows() method, as shown in the code syntax below.
The <TABLE_INSTANCE> used in the code below is the instance defined in the Table Instance page.
copyFuture<(APIResponse, List<ZCatalystRow>)> <TABLE_INSTANCE>.createRows(List<ZCatalystRow> rows)
Parameters:
- rows: The array of rows to be created
A sample code snippet with creating row instances is shown below:
copyList<ZCatalystRow> newRows = []; //Create an array //Define row instances for rows to be created ZCatalystRow row1 = dataStore.getTableInstance(identifier: 'Products').newRow(); row1.setColumnValue('product_name', 'a'); row1.setColumnValue('product_price', 25); row1.setColumnValue('product_quantity', 50); ZCatalystRow row2 = dataStore.getTableInstance(identifier: 'Products').newRow(); row2.setColumnValue('product_name', 'b'); row2.setColumnValue('product_price', 30); row2.setColumnValue('product_quantity', 40); ZCatalystRow row3 = dataStore.getTableInstance(identifier: 'Products').newRow(); row3.setColumnValue('product_name', 'c'); row3.setColumnValue('product_price', 25); row3.setColumnValue('product_quantity', 70); //Add row instances to the array newRows.add(row1); newRows.add(row2); newRows.add(row3); try{ var (response, rows) = await dataStore.getTableInstance(identifier: 'Products').createRows(rows: newRows); for (var row in rows) { print(row.id); //Actions to be executed upon a successful creation of the rows } } on ZCatalystException catch (ex) { print('The IDs of the rows that were successfully created are listed below:') }
Last Updated 2024-09-12 18:16:13 +0530 +0530
Yes
No
Send your feedback to us