So I'm having trouble sorting out how to dynamically add data to my DataGrid. I'm getting help from Syncfusion support who have been phenomenal so far. However I still haven't been able to get full and proper functionality.
I am using GetX GetBuilder to wrap the DataGrid. It receives data from the database and should be updating this data when the database is added to. However it is currently running continuously, or say 50X per second. This is obviously not good in many ways. However I currently have full functionality but it's not proper.
I threw a print statement into the GridSource constructor and it prints continuously. So something is wrong and I can't figure it out. I have tried moving the Builder around (out of the builder) as well as the dataSource declaration however this kills the function of the grid loading at all.
Here is the full demo project here
Can someone please point out what I'm doing wrong?
DataGrid screen:
import 'package:flutter/material.dart';
import 'package:getx_hive_demo_one/presentation/event_list_screen/add_event_button.dart';
import 'package:get/get.dart';
import 'package:getx_hive_demo_one/database/database.dart';
import 'package:getx_hive_demo_one/models/event.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
class EventListScreen extends StatelessWidget {
final Database database = Get.put(Database());
GridSource dataSource;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Event Dashboard'),
backgroundColor: Colors.grey[850],
actions: [AddEventButton(source: dataSource)],
),
body: SafeArea(child: GetBuilder<Database>(
builder: (database) {
dataSource = GridSource(database.getEventList());
return SfDataGrid(
source: dataSource,
columnWidthMode: ColumnWidthMode.fill,
columns: <GridColumn>[
GridTextColumn(
columnName: 'name',
label: Container(
color: Colors.grey[800],
padding: EdgeInsets.all(8.0),
alignment: Alignment.centerLeft,
child: Text(
'Event',
style: TextStyle(color: Colors.white),
))),
GridTextColumn(
columnName: 'start',
label: Container(
color: Colors.grey[800],
padding: EdgeInsets.all(8.0),
alignment: Alignment.centerLeft,
child: Text(
'Start Date',
style: TextStyle(color: Colors.white),
))),
GridTextColumn(
columnName: 'duration',
label: Container(
color: Colors.grey[800],
padding: EdgeInsets.all(8.0),
alignment: Alignment.centerLeft,
child: Text(
'Event Duration',
style: TextStyle(color: Colors.white),
overflow: TextOverflow.ellipsis,
))),
GridTextColumn(
columnName: 'invitees',
label: Container(
color: Colors.grey[800],
padding: EdgeInsets.all(8.0),
alignment: Alignment.centerLeft,
child: Text(
'Attendees',
style: TextStyle(color: Colors.white),
))),
GridTextColumn(
columnName: 'location',
label: Container(
color: Colors.grey[800],
padding: EdgeInsets.all(8.0),
alignment: Alignment.centerLeft,
child: Text(
'Location',
style: TextStyle(color: Colors.white),
))),
],
);
},
)));
}
}
class GridSource extends DataGridSource {
List<DataGridRow> _eventGridRow = [];
List<Event> eventGrid = [];
GridSource(this.eventGrid) {
print('Run');
buildDataGridRows();
}
void buildDataGridRows() {
_eventGridRow = eventGrid
.map<DataGridRow>(
(event) => DataGridRow(
cells: [
DataGridCell<String>(columnName: 'name', value: event.eventName),
DataGridCell<String>(columnName: 'start', value: event.eventStart.toString()),
DataGridCell<String>(columnName: 'duration', value: event.eventDuration.toString()),
DataGridCell<String>(columnName: 'invitees', value: event.numberInvitees.toString()),
DataGridCell<String>(columnName: 'location', value: event.location),
],
),
)
.toList();
}
@override
List<DataGridRow> get rows => _eventGridRow;
@override
DataGridRowAdapter buildRow(DataGridRow row) {
final int index = _eventGridRow.indexOf(row);
Color getRowBackgroundColor() {
if (index % 2 == 0) {
return Colors.grey[100];
}
return Colors.transparent;
}
return DataGridRowAdapter(
color: getRowBackgroundColor(),
cells: [
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.all(8.0),
child: Text(
row.getCells()[0].value ?? '',
overflow: TextOverflow.ellipsis,
),
),
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.all(8.0),
child: Text(
row.getCells()[1].value ?? '',
overflow: TextOverflow.ellipsis,
),
),
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.all(8.0),
child: Text(
row.getCells()[2].value ?? '',
overflow: TextOverflow.ellipsis,
),
),
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.all(8.0),
child: Text(
row.getCells()[3].value ?? ' ',
overflow: TextOverflow.ellipsis,
),
),
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.all(8.0),
child: Text(
row.getCells()[4].value ?? '',
overflow: TextOverflow.ellipsis,
),
),
],
);
}
void updateDataGridSource() {
notifyListeners();
}
}
Continuous print statement:
flutter: Run
flutter: Run
flutter: Run
flutter: Run
flutter: Run
flutter: Run
flutter: Run
flutter: Run
flutter: Run
flutter: Run
flutter: Run
flutter: Run
flutter: Run