0

我是新手,我正在使用包 grouped_list 将列表与 SQLite 数据库中的数据分组,grouped_list 与现有列表一起工作,就像在它的示例中一样,但是当我用从 SQLite 数据库检索的数据替换它等于列表时,它不会'不工作,我得到错误:

The following NoSuchMethodError was thrown building GroupedListView<dynamic, String>(dirty, state: _GroupedListViewState<dynamic, String>#c2ba2):
Class 'DrinkDatabase' has no instance method '[]'.
Receiver: Instance of 'DrinkDatabase'
Tried calling: []("drinkGroup")

下面是我的代码。如果您需要更多信息,请告诉我,请帮助,谢谢!

GroupedListView<dynamic, String>(
  shrinkWrap: true,
  elements: homeController.drinkList,
  groupBy: (element) => element['drinkGroup'],
  groupComparator: (value1, value2) => value2.compareTo(value1),
  itemComparator: (item1, item2) =>
      item1['drinkDateTime'].compareTo(item2['drinkDateTime']),
  order: GroupedListOrder.DESC,
  useStickyGroupSeparators: true,
  groupSeparatorBuilder: (String value) =>
      Container(),
  itemBuilder: (c, element) {
    return Container();
  },
)

这就是我声明列表的方式:

List drinkList = List<DrinkDatabase>();

和类 DrinkDatabase:

class DrinkDatabase {
  int drinkId;
  int drinkVolume;
  String drinkGroup;
  String drinkCategory;
  String drinkDateTime;

  DrinkDatabase({
    this.drinkId,
    this.drinkVolume,
    this.drinkGroup,
    this.drinkCategory,
    this.drinkDateTime,
  });

  Map<String, dynamic> toMap() {
    return {
      "drinkId": drinkId,
      "drinkVolume": drinkVolume,
      "drinkGroup": drinkGroup,
      "drinkCategory": drinkCategory,
      "drinkDateTime": drinkDateTime,
    };
  }

  @override
  String toString() {
    return 'drinkTable('
        '"drinkId": ${this.drinkId}, '
        '"drinkVolume": ${this.drinkVolume}, '
        '"drinkGroup": ${this.drinkGroup}, '
        '"drinkCategory": ${this.drinkCategory}, '
        '"drinkDateTime": ${this.drinkDateTime}, ';
  }
}
4

1 回答 1

0

我做了什么,它对我有用

申报清单的方式

List drinkList = [];

然后换行

elements: homeController.drinkList,

elements: drinkList,

从数据库查询结果列表构建列表

Future<void> getSampleList() async {
    drinkList.clear();
    List<Map<String, dynamic>> result =
        await DrinkDatabaseHelper.instance.queryAll();
    print('result= $result');
    var map = new Map<String, dynamic>();
    for (int i = 0; i < result.length; i++) {
      map['drinkId'] = result[i]['drinkId'].toString();
      map['drinkVolume'] = result[i]['drinkVolume'].toString();
      map['drinkGroup'] = result[i]['drinkGroup'];
      map['drinkCategory'] = result[i]['drinkCategory'];
      map['drinkDateTime'] = result[i]['drinkDateTime'];
      drinkList.add(Map<dynamic, String>.from(map));
    }

在 DrinkDatabaseHelper 中

Future<List<Map<String, dynamic>>> queryAll() async {
    Database db = await instance.database;
    return await db.query(_tableName);
  }

如果您有更多问题,请告诉我

于 2021-04-05T05:08:20.413 回答