Home > Blockchain >  Class 'BookModel' has no instance method '[]'
Class 'BookModel' has no instance method '[]'

Time:06-25

I'm trying to create Paginated Data Table.But I am getting error I have searched for similar theads on Stackoverflow but couldnt find a solution.. My programming knowladge is very limited.So I dont know how to fix it and where this error come from .Thanks for help

This is Mydata Class that extends DataTableSource

class MyData extends DataTableSource {
  var controller = Get.find<Controller>();
  var _data;
  MyData() {
    _data = controller.books;
  }
  @override
  DataRow? getRow(int index) {
    return DataRow(
      cells: [
        DataCell(Text(_data[index]['name'].toString())),
        DataCell(Text(_data[index]['writer'].toString())),
      ],
    );
  }

This is BookModel class

class BookModel{
  String _name;
  String _writer;
  String _img;
  String _price;
  String _webSite;


  BookModel(this._name, this._writer, this._img, this._price, this._webSite);

This is CreateTable class

  var controller = Get.find<Controller>();
  final DataTableSource _data = MyData() ;
  @override
  Widget build(BuildContext context) {
    return bodyData();
  }

  Widget bodyData() => PaginatedDataTable(
    source: _data,

    columns: const [

      DataColumn(label: Text('WebSite')),
      DataColumn(label: Text('Book Name'))
 

CodePudding user response:

Can you try _data[index].name and why do you need that toString(), I assume that name is already a string. And in models class why you user _ remove that and try like _data[index].name.

CodePudding user response:

  • Your model BookModel's attributes are private, this means that they are inaccessible from outside the .dart file that contains the class
  • using [index]['key'] works only if your data list is a List<Map> but your data list is List, so your code should be DataCell(Text(_data[index]._name.)),
    Note that it won't work if the BookModel class is in another dart file and your need to change the fields names to be publish without the leading _, and then your code should be DataCell(Text(_data[index].name.))
  • Related