1

My flutter data model uses UniqueKey to generate unique keys for firebase documents:

final String uKey = UniqueKey().toString();
    DocumentReference documentReferencer =
        _mainCollection.doc(userUid).collection('teams').doc(uKey);

Typically this generates a key e.g. [#81215]

This has been working for a number of years now.

I rebuilt the app with flutter 3.22.1 and now on IOS the same code is generating a single key, literally: Instance of 'UniqueKey'.

This is not occurring on Android where my test device is generating unique keys as before.

I'm using Xcode 15.4 on an iMac running Sonoma 14.5

Pretty much every collection I have in the app uses this method to create a unique key so the option to replace UniqueKey with an alternative is not so simple.

As far as my googling goes I can't see any other reports of this issue. Therefore creating this question to check whether anyone has hit the same problem and hopefully solved it.

3
  • FYI, there is no guarantee that UniqueKey().toString() return unique strings anyway. As with all hash codes, there is the possibility for a hash collision, especially when the hash code is being truncated to 5 hex digits.
    – jamesdlin
    Commented Jul 7 at 0:20
  • Why don't you use collection().add(), which generates a document reference with a unique ID for you? This is exceptionally common, and is well-documented. Commented Jul 7 at 1:44
  • Hash collisions are v unlikeIy as this is a collection within a collection and small no of records. I don't get why I'm getting different behaviours in IOS and Android with the same package. But if .add() adds a unique key @DougStevenson then I'll go for that. Thanks all.
    – Martin
    Commented Jul 7 at 12:25

0