如何向扩展DataTableSource的类提供数据

时间:2018-12-11 06:45:04

标签: dart flutter

使用PaginatedDataTable在浮动显示数据

我必须提供一个源,它是从DataTableSource扩展的类 但是我无法在类上提供任何数据,因为我尝试通过错误

扩展了DataTableSource

“只能在初始化初始化器中访问静态成员”

那么我如何从外部提供DataTableSource的初始数据?

class TransactionAcrossMonthsItem {
  final String transactionName;
  final int monthOf1;
  final int monthOf2;
  final int monthOf3;
  final int monthOf4;
  final int monthOf5;
  final int monthOf6;
  final int monthOf7;
  final int monthOf8;
  final int monthOf9;
  final int monthOf10;
  final int monthOf11;
  final int monthOf12;
  final int totalCount;
  final double percent;
  bool selected = false;
  TransactionAcrossMonthsItem(
      this.transactionName,
      this.monthOf1,
      this.monthOf2,
      this.monthOf3,
      this.monthOf4,
      this.monthOf5,
      this.monthOf6,
      this.monthOf7,
      this.monthOf8,
      this.monthOf9,
      this.monthOf10,
      this.monthOf11,
      this.monthOf12,
      this.totalCount,
      this.percent);
}

class TransactionsAcrossDataSource extends DataTableSource {
  final List<TransactionAcrossMonthsItem> _data;

  int _selectedCount = 0;

  TransactionsAcrossDataSource(this._data);
...........
}




class DetailedReportPage extends StatefulWidget {
  final int yearOf;
  DetailedReportPage({this.yearOf});

  @override
  State<StatefulWidget> createState() {
    return _DetailedReportPageState();
  }
}

class _DetailedReportPageState extends State<DetailedReportPage> {
  int _rowsPerPage = PaginatedDataTable.defaultRowsPerPage;
  int _sortColumnIndex;
  bool _sortAscending = true;

   List<TransactionAcrossMonthsItem> _data = [
    TransactionAcrossMonthsItem(
        'eDirham', 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1000, 70),
    TransactionAcrossMonthsItem(
        'tasheel', 120, 120, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 2000, 20),
    TransactionAcrossMonthsItem(
        'immgration', 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 3000, 10)
  ];
  var _dataSource = TransactionsAcrossDataSource(_data);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: .....
      body: createReport(),
    );
  }



  Widget createReport() {
    return ListView(padding: const EdgeInsets.all(20.0), children: <Widget>[
      PaginatedDataTable(
        header: Text('Year of ${widget.yearOf}'),
        rowsPerPage: _rowsPerPage,
        onRowsPerPageChanged: (int value) {
          setState(() {
            _rowsPerPage = value;
          });
        },
        sortColumnIndex: _sortColumnIndex,
        sortAscending: _sortAscending,
        // onSelectAll: _dataSource._selectAll,
        columns: getColumns(),
        source: _dataSource,
      )
    ]);
  }

  List<DataColumn> getColumns() {
    return [
      DataColumn(
        label: Text('Name'),
        ......
      ),
      DataColumn(
        .....
      ),
      ......
    ];
  }
}

1 个答案:

答案 0 :(得分:0)

更改

 var _dataSource = TransactionsAcrossDataSource(_data);

TransactionsAcrossDataSource _dataSource; 

然后移动

_dataSource = TransactionsAcrossDataSource(_data); 

构造函数。

因为您不能在类的方法或构造函数之外拥有随机代码。