Flutter - How to display data from firebase to Data table? Also My function in class is not accessible from other class

Viewed 26

I have a class as follows:

class AttendanceDataTable extends DataTableSource {
  List<DocumentSnapshot> datas = <DocumentSnapshot>[];
  List<Map<String, dynamic>> _data = [];

  final user = FirebaseAuth.instance.currentUser;

  final CollectionReference _users =
      FirebaseFirestore.instance.collection('users');

  @override
  DataRow? getRow(int index) {
    return DataRow(cells: [
      DataCell(Text(_data[index]['date'].toString())),
      DataCell(Text(_data[index]['inTime'].toString())),
      DataCell(Text(_data[index]['outTime'].toString())),
      DataCell(Text(_data[index]['uid'].toString())),
    ]);
  }

  @override
  bool get isRowCountApproximate => false;

  @override
  int get rowCount => _data.length;

  @override
  int get selectedRowCount => 0;

  Future<void> getData() async {
    QuerySnapshot query =
        await _users.doc(user!.uid).collection('attendance').get();
    datas.addAll(query.docs);

    _data = datas.cast<Map<String, dynamic>>();
  }
}

I am initializing this class from my attendance page where I am showing Paginated DataTable.

DataTableSource dt = new AttendanceDataTable();

My paginatedDataTable looks like this inside body of Scaffold Widget:

body: Stack(children: [
        SingleChildScrollView(
          child: Column(
            children: [
              PaginatedDataTable(columns: const [
                DataColumn(label: Text('Date')),
                DataColumn(label: Text('In')),
                DataColumn(label: Text('Out')),
                DataColumn(label: Text('Hrs')),
              ], source: dt)
            ],
          ),
        )
      ]),

I want to call dt.getData() inside initState(). But getData() is not available here.

  1. Why Am I not able to access getData() function?
  2. Am I using the correct way to populate data from firebase firestore to DataTable?

All the examples I search shows List.generate method to populate datatable, instead I want data from firebase.

Can anyone help me?

0 Answers
Related