Last active
August 1, 2023 23:50
-
-
Save wilsonsilva/cc1f612f45228bfad5837a19a317d2fc to your computer and use it in GitHub Desktop.
Create a Firestore ID in Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
const _characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
const _length = 28; | |
String createFirestoreId() { | |
return List.generate(_length, _randomCharacter).join(); | |
} | |
String _randomCharacter(_) { | |
return _characters[Random.secure().nextInt(_characters.length)]; | |
} | |
void main() { | |
print(createFirestoreId()); // Prints a string like 'UhN7eDazLYe8QNMdtyCVzNMrU1t1' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DartPad: https://dartpad.dev/?id=cc1f612f45228bfad5837a19a317d2fc
Alternatively, you can use this hack by Luke Pighetti: