How to pass data from class of extends DataTableSource to StateFull Class

3.1k views Asked by At

I have PaginatedDataTable and I put property of source's PaginatedDataTable to another class.. here is the code

class MainPage extends StatefulWidget {

  MainPage({Key key}) : super(key: key);

  @override
  _MainPageState createState() => new _MainPageState();
}

class _MainPageState extends State<MainPage> {
  @override
  Widget build(BuildContext context) {
   return Scaffold(
body: PaginatedDataTable(
        header: Center(
                            child: Text(
                          'List Data'
                        )),
                        onRowsPerPageChanged:...
                        columns: <DataColumn>[
                          DataColumn(label: Text('No.')),
                          DataColumn(label: Text('Data 1')),
                          DataColumn(label: Text('Data 2'))
                        ],
                        source: mysource,
                        rowsPerPage:...
                      )

   )
  }
}

and for the mysource:

class MySource extends DataTableSource {
...
}

but the thing that makes me confused is... after I process my data inside class MySource extends DataTableSource I don't know how to pass the data from there to call it inside MainPage of StateFulWidget.. so is there a way to call data inside class MySource extends DataTableSource from class MainPage extends StatefulWidget

1

There are 1 answers

8
Mevlüt Gür On

Write code for your question ,I hope that will helpful.

class _MainPageState extends State<MainPage> {
      @override
      Widget build(BuildContext context) {
        MySource mySource = new MySource(["ab","bc","de","ef"]);
        return Scaffold(
          body: Column(
          children: [
            //Widget form
            PaginatedDataTable(
              header: Center(child: Text('List Data')),
              columns: <DataColumn>[
                DataColumn(label: Text('No.')),
                DataColumn(label: Text('Data 1')),
                DataColumn(label: Text('Action')),
              ],
              source: mySource,
            ),
          ],
        ));
      }
    }
    
    class MySource extends DataTableSource {
      List<String> value;
      MySource(this.value) {
        print(value);
      }
    
      @override
      DataRow getRow(int index) {
        // TODO: implement getRow
        return DataRow.byIndex(
              index: index,
              cells: [
                DataCell(Text('$index')),
                DataCell(Text(value[index])),
                DataCell(InkWell(
          onTap:(){
            //fill the form above the table and after user fill it, the data inside the table will be refreshed
          },
          child: Text("Click"),
        ),),
              ],);
      }
    
      @override
      // TODO: implement isRowCountApproximate
      bool get isRowCountApproximate => false;
    
      @override
      // TODO: implement rowCount
      int get rowCount => value.length;
    
      @override
      // TODO: implement selectedRowCount
      int get selectedRowCount =>0;
    }

enter image description here