Articles in this section

How to Add a Computed Column to Flutter DataTable?

In this article, we will show you how to add a computed column to a Flutter DataTable.

Initialize the SfDataGrid widget with all the required properties. Create a model class that includes the necessary properties, along with a computed property using a getter. This computed property does not store data directly; instead, it calculates and returns values based on other properties of the class. Additionally, you need to specify the columns in DataGrid, including the computed column.

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Syncfusion Flutter DataGrid'),
      ),
      body: SfDataGrid(
        source: employeeDataSource,
        columnWidthMode: ColumnWidthMode.fill,
        columns: <GridColumn>[
          GridColumn(
            columnName: 'id',
            label: Container(
              padding: const EdgeInsets.all(16.0),
              alignment: Alignment.center,
              child: const Text('ID'),
            ),
          ),
          GridColumn(
            columnName: 'name',
            label: Container(
              padding: const EdgeInsets.all(16.0),
              alignment: Alignment.center,
              child: const Text('Name'),
            ),
          ),
          GridColumn(
            columnName: 'credit',
            label: Container(
              padding: const EdgeInsets.all(16.0),
              alignment: Alignment.center,
              child: const Text('Credit'),
            ),
          ),
          GridColumn(
            columnName: 'debit',
            label: Container(
              padding: const EdgeInsets.all(16.0),
              alignment: Alignment.center,
              child: const Text('Debit'),
            ),
          ),
          GridColumn(
            columnName: 'balance',
            label: Container(
              padding: const EdgeInsets.all(16.0),
              alignment: Alignment.center,
              child: const Text('Balance'),
            ),
          ),
        ],
      ),
    );
  }

class Employee {
  Employee(this.id, this.name, this.credit, this.debit);

  final int id;
  final String name;
  final double credit;
  final double debit;

  double get balance => credit - debit; // Computed balance
}

View the GitHub sample here.

Conclusion
I hope you enjoyed learning how to add a computed column to Flutter DataTable.
You can refer to our Flutter DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flatter DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloadspage. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied