Reputation: 153
So, I'm trying to group sort a list by a particular method of an class. My code currently stores "meals" with Hive by this class format:
import 'package:hive/hive.dart';
part 'meal.g.dart';
@HiveType(typeId: 1, adapterName: "MealAdapter")
class MealModel{
@HiveField(0)
int id;
@HiveField(1)
String mealName;
@HiveField(2)
int calories;
@HiveField(3)
int carbohydrates;
@HiveField(4)
int protein;
@HiveField(5)
int fats;
@HiveField(6)
String day;
MealModel(this.id, this.mealName, this.calories, this.day, [this.carbohydrates, this.protein, this.fats]);
}
The problem is when I have no idea I would pass values to each of the parameters for a GroupedListView (https://pub.dev/packages/grouped_list) to display the stored meals:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:jaetrack/colors.dart';
import 'package:jaetrack/dbmodel/meal.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:grouped_list/grouped_list.dart';
class Meals extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MealsState();
}
}
class MealsState extends State<Meals> {
Box<MealModel> mealBox = Hive.box<MealModel>('meals');
MealModel previousMeal;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: mainPurp,
body: SingleChildScrollView(
child: Column(
children: [
ValueListenableBuilder(
valueListenable: Hive.box<MealModel>('meals').listenable(),
builder: (context, Box<MealModel> box, _) {
final mealList = box.values.toList().cast<MealModel>();
print(mealList);
return GroupedListView<dynamic, String>(
elements: mealList,
groupBy: (meal) => meal, // no idea how to sort by calories, name etc. of my MealModel class,
groupSeparatorBuilder: (String groupByValue) =>
Text(groupByValue), // date value
itemBuilder: (context, dynamic element) =>
Text(element['name']), // this is where I would put the meal names/calories etc.
itemComparator: (item1, item2) =>
item1['name'].compareTo(item2['name']), // sort by date is what I'm looking for but again I don't know how to access
useStickyGroupSeparators: true,
floatingHeader: true,
order: GroupedListOrder.ASC,
);
})
],
),
),
);
}
}
So if I still don't make sense, I cast the values of my hive box of meals as instances of the MealModel above. So the list would be like [Instance of MealModel, Instance of MealModel, ...] I just need to know how to access the values inside of the instance of the MealModel to pass to the GroupedList.
Upvotes: 0
Views: 153
Reputation: 1161
package:grouped_list
library are using dynamic
in all of their examples to simplify usage for people who don't get generics. but it's misleading in your case, you're losing the strong typing.
cast to
return GroupedListView<MealModel, String>(
instead of
return GroupedListView<dynamic, String>(
then all of your element
, meal
and item2
vars will be of MealModel
and you can do element.mealName
, and meal.calories
to group by calories
for the sorting question, simply sort mealList
before passing it to the GroupedListView
Upvotes: 1