How could I use ReorderableListView when displaying a list of potentially identical items?

257 Views Asked by At

The ReorderableListView widget requires all children to have a unique Key.

I wish to display a reorderable list of plugins in use in my application. One feature of this application's design is that the same plugin can be added to the plugin list in multiple positions (and by same, I mean the same, identically equal object).

Due to this possibility of the same plugin appearing twice in the list, the only property unique to each list item is its position. This is not an adequate value to use in a Key, though, as it changes when items are re-ordered; avoiding this is the exact reason why a Key is required in the first place.

How could I use a ReorderableListView in this scenario?

1

There are 1 best solutions below

2
On

This seems like a programming problem more than a framework (Flutter) problem. If you require to have a unique key maybe you need to wrap it in a class that has your data an a second ID (the original position) so when its reordered even if the position of the UI changes because the list is reordered the original value won't.

Class Wrapper<T> {
    final T data;
    final int uuid; ///or it could be a unique String name or a composition key of the place in the list with the data id

    const Wrapper(this. data, this.uuid);

}

final List<Wrapper<Plugin>> list = [];

for (int i = 0; i < plugins.length; i++) {
   list.add(Wrapper<Plugin>(plugins(i), i));
}

So now even when you reorder this list you won't redo the list, but just move them around. If you require to save the order you will find that its almost as a composite key of 2 tables in SQL, the table of your data, and a simple table that defines the order of that data as the user wants. (right now is as it comes, but you could change that)