I want to create a function that generates a random string in dart. It should include alphabets and numbers all mixed together. How can I do that?
Asked
Active
Viewed 6.9k times
5 Answers
184
Or if you don't want to use a package you can make a simple implementation like:
import 'dart:math';
void main() {
print(getRandomString(5)); // 5GKjb
print(getRandomString(10)); // LZrJOTBNGA
print(getRandomString(15)); // PqokAO1BQBHyJVK
}
const _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
Random _rnd = Random();
String getRandomString(int length) => String.fromCharCodes(Iterable.generate(
length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));
I should add that you should not use this code to generate passwords or other kind of secrets. If you do that, please at least use Random.secure()
to create the random generator.

Dmytro Rostopira
- 10,588
- 4
- 64
- 86

julemand101
- 28,470
- 5
- 52
- 48
39
Option A with charCodes:
import 'dart:math';
String generateRandomString(int len) {
var r = Random();
return String.fromCharCodes(List.generate(len, (index) => r.nextInt(33) + 89));
}
Generates random string using visible characters including special ones.
Option B with a predefined string:
import 'dart:math';
String generateRandomString(int len) {
var r = Random();
const _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
return List.generate(len, (index) => _chars[r.nextInt(_chars.length)]).join();
}

Arif Amirani
- 26,265
- 3
- 33
- 30
-
3Be aware that generating the `Random` object for each `String` is not really the best way. The reason for this is `Random()` will get a new generator based on a new seed generated internally which only has 32 bit of random. This makes it (if you generates a lot of random Strings) likely you get the same seed and therefore will generate the exact same `String`. This is the reason why I did put the `Random` object outside of my method so we don't create a new seeded random generator constantly. This is not a problem since the random generator is very good without the risks of looping. – julemand101 Mar 27 '21 at 14:58
-
2I should add that `Random.secure()` does not have the same problem since every random number from this generator comes from the OS. But you could still prevent an object to be created by caching a single instance. – julemand101 Mar 27 '21 at 14:59
-
1Thank you :) @julemand101 – leylekseven Dec 15 '21 at 07:16
24
Found this in a blog article about crypto strings:
import 'dart:math';
import 'dart:convert';
String getRandString(int len) {
var random = Random.secure();
var values = List<int>.generate(len, (i) => random.nextInt(255));
return base64UrlEncode(values);
}
The string always ends with ==
. I would also assume that it's not the fastest solution.
But you don't need third party packages and don't have to declare obscure constants.

jnnks
- 866
- 6
- 17
9
import 'dart:math';
String generateRandomString(int len) {
var r = Random();
String randomString =String.fromCharCodes(List.generate(len, (index)=> r.nextInt(33) + 89));
return randomString;
}

Söhrab Vahidli
- 587
- 7
- 10
3
If you care about the uniqueness and security of your random string, you can use UUID package easily
var uuid = Uuid();
// Generate a v1 (time-based) id
uuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'
// Generate a v4 (random) id
uuid.v4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'

Arash
- 825
- 2
- 11
- 19