embraceitmobile / animated_tree_view

Animated TreeView based on AnimatedList allows building fully customizable Nodes that can be nested to infinite levels and children.
https://pub.dev/packages/animated_tree_view
MIT License
59 stars 29 forks source link

Not generating unique keys for the nodes #29

Closed busslina closed 10 months ago

busslina commented 11 months ago

I got this error (https://github.com/embraceitmobile/animated_tree_view/blob/3e7ec1e132a54a07d5ae9e711cce98cc3721bdc5/lib/node/node.dart#L54) when I have a Node with a lot of children.

In my case it happens (sometimes) when I open this tree location (/bin/), where there are a lot of children.

I bypass this error by providing a String key to every node.

I suggest that maybe you can provide an int instead of String (2^64 unique keys is enough).

Also I tried to use int keys like this: '$keyIndex++', but I got same error: (0 key is yet used, 1 key is yet used, ...).

jawwad-hassan89 commented 11 months ago

Do you get the DuplicateKeyException with the default keys or with the custom keys? Using an int key would not work, as the keys are used to compute the path, so any Key, will need to have a toString() override. If its an issue with the default implementation, then rather then using the UniqueKey, I can update it to use a UUID instead.

You can test your code by using one the of UUID, let me if it resolves the issue for you.

jawwad-hassan89 commented 11 months ago

I looked into this issue, and apparently, if the child count is more than 10,000, then the duplicate keys started to appear at a rate of 0.3-0.4%. I have replaced the UniqueKey with Uuid for generating the keys, and now the issue has been resolved. There are no duplicate keys even at 100,000 children.

https://github.com/jawwad-hassan89/animated_tree_view/blob/d71497b6e6107a2ab356d54d38d3f24a3b4c6f0a/test/node/node_test.dart#L50

busslina commented 11 months ago

Yeah, it was happening when opening large directories multiple times. The best solution would be with no limit. Why you don't give a try to GlobalKey?

I read somewhere that is not real guaranty of uniqueness with uuid

jawwad-hassan89 commented 11 months ago

Uuids are guaranteed to be unique within a system. As we are only concerned with uniqueness within the local system that is generating the Uuids, so they will be unique. Theoretically, there is no limit to how many Uuids can be generated, however I have only tested upto 100,000 entries.

Relevant ancillary documentation: [1][2] Identifier uniqueness considerations: This document specifies three algorithms to generate UUIDs: the first leverages the unique values of 802 MAC addresses to guarantee uniqueness, the second uses pseudo-random number generators, and the third uses cryptographic hashing and application-provided text strings. As a result, the UUIDs generated according to the mechanisms here will be unique from all other UUIDs that have been or will be assigned.

A Universally Unique IDentifier (UUID) URN Namespace