When changing the order of categories, things don’t work as expected. ReorderableListView is used to change the order, but instead of changing the order normally, new data is created. I defined the object as immutable using frozen, but I think giving up immutable would be the way to go. Is there a better way?
The code is below:
Future<void> reorderCategory(int oldIndex, int newIndex) async {
final categories = await getCategories();
await isar.writeTxn(() async {
final targetOrder = categories[newIndex].order!;
final sourceOrder = categories[oldIndex].order!;
await isar.categorys.put(categories[oldIndex].setOrder(targetOrder));
await isar.categorys.put(categories[newIndex].setOrder(sourceOrder));
});
}
@freezed
@Collection(ignore: {'copyWith'})
class Category with _$Category {
Category._();
factory Category({
required String name,
int? order,
}) = _Category;
Id get id => Isar.autoIncrement;
final memos = IsarLinks<Memo>();
}
extension MutableCategory on Category {
Category setOrder(int order) {
return copyWith(order: order);
}
}