flutter - Flutter Hive:GroupBy 列表按日期
问题描述
我正在使用 Hive Flutter。我有一个这样的结果列表,但我想要 GroupBy 按日期列出的列表。
结果是我想要的,如下所示:
2019 年 12 月 9 日,星期一
- ggh
- ggh
- ggh
- ggh
我已经研究并找到了一些包:Collection Package。我尝试使用此脚本对列表进行分组,但打印不是我想要的:
var groupData = groupBy(historyList, (obj) => historyList);
print(historyList);
结果
I/flutter (23894): {2019-12-09 01:08:56.328: [Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive']}
I/flutter (23894): {2019-12-09 00:57:22.455: [Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive']}
I/flutter (23894): {2019-12-09 00:57:01.274: [Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive']}
I/flutter (23894): {2019-12-09 00:56:56.992: [Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive']}
I/flutter (23894): {2019-12-09 00:56:47.549: [Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive', Instance of 'HistoryModelHive']}
这是我的模型:
WatchBoxBuilder(
box: Hive.box("history_box"),
builder: (ctx, boxx) {
final historyList = boxx.values.toList().cast<HistoryModelHive>();
historyList.sort((first, end) =>
end.dateHistoryCreate.compareTo(first.dateHistoryCreate));
if (historyList.isEmpty) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Image.asset(
"assets/images/empty2.png",
fit: BoxFit.cover,
height: 250,
),
Text(
'Your History Empty',
textAlign: TextAlign.center,
style: textTheme.display1,
)
],
);
} else {
return ListView.builder(
itemCount: boxx.length,
itemBuilder: (BuildContext context, int i) {
final historyData = historyList[i];
var groupData = groupBy(
historyList, (obj) => historyData.dateHistoryCreate);
print(groupData);
return ListViewHistory(
id: historyData.id,
receiverName: historyData.receiverName,
amountDebt: historyData.amountDebt,
amountLack: historyData.amountLack,
amountSubstract: historyData.amountSubstract,
dateHistoryCreate: historyData.dateHistoryCreate,
imageReceiver: historyData.imageReceiver,
imageSignature: historyData.imageSignature,
nameAction: historyData.nameAction,
);
},
);
}
},
)),
解决方案
您可以使用包https://pub.dev/packages/grouped_listview
您可以在下面复制粘贴运行完整代码
代码片段
List<History> historyList = [History("Monday, December 9,2019", "gghh"), History("Monday, December 11,2019", "1"), History("Monday, December 10,2019", "2"), History("Monday, December 9,2019", "gghh"),History("Monday, December 9,2019", "gghh"),History("Monday, December 10,2019", "5") ];
@override
Widget build(BuildContext context) {
return GroupedListView<History, String>(
collection: historyList,
groupBy: (History g) => g.dateHistoryCreate,
listBuilder: (BuildContext context, History g) => ListTile(title: Text(g.amountDebt.toString())),
groupBuilder: (BuildContext context, String name) => Text(name),
);
}
演示
完整代码
import 'package:flutter/material.dart';
import 'package:grouped_listview/grouped_listview.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(flex: 1, child: ExampleWidget()),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class History {
String dateHistoryCreate;
String amountDebt;
History(this.dateHistoryCreate, this.amountDebt);
}
class ExampleWidget extends StatelessWidget {
List<History> historyList = [History("Monday, December 9,2019", "gghh"), History("Monday, December 11,2019", "1"), History("Monday, December 10,2019", "2"), History("Monday, December 9,2019", "gghh"),History("Monday, December 9,2019", "gghh"),History("Monday, December 10,2019", "5") ];
@override
Widget build(BuildContext context) {
return GroupedListView<History, String>(
collection: historyList,
groupBy: (History g) => g.dateHistoryCreate,
listBuilder: (BuildContext context, History g) => ListTile(title: Text(g.amountDebt.toString())),
groupBuilder: (BuildContext context, String name) => Text(name),
);
}
}
推荐阅读
- java - 尝试用maven安装Jsoup,
- c++ - 用 %s 调用 printf 并传递一个零长度的 char* 是未定义的行为吗?
- php - SQL 调用 - 这是正确的吗?
- c# - 星号大小在 WPF 网格中不起作用
- r - 减少ggplot中图形的大小 - R
- constraints - 使用 getVarByName() 向 gurobi 添加约束
- sql - 通过增量 postgres 求和
- c - 使用远程线程进行 DLL 代码注入:将 DLL 文件名/位置存储在目标进程地址空间的什么位置?
- scala - 面向新手的 UDF 问题中的 Java 字符串
- angularjs - 使用 `$mdDialog.show` 使用第二个组件更改一个组件中的值