How To Easily Generate Dart Random String – Easy Flutter Guide

dart random string

In this tutorial, we’ll learn how to properly generate Dart random string by using practical Dart code example.

Introduction: Dart Random String

It specifies the process through which a string of random characters will be returned. We’ll also allow the code to return a specific length of Dart random string.

Implementing Dart Random String (Easy Example Code)

We’ll now implement a function which will generate and return a random string of alphabets and numbers everytime its called. See below steps:

Step 1: import dart:math

import 'dart:math';   // in order to use random function

Step 2: Define the Function which will Generate Random String

String generateRandomString(int lengthOfString){
    final random = Random();
    const allChars='AaBbCcDdlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1EeFfGgHhIiJjKkL234567890';

   // below statement will generate a random string of length using the characters 
   // and length provided to it
    final randomString = List.generate(lengthOfString, 
                          (index) => allChars[random.nextInt(allChars.length)]).join();
    return randomString;    // return the generated string

Step 3: Called this Function

String randomStringReturned= generateRandomString(6); // can store the returned value in string variable


So by using this function, we can easily generate a Dart random string of any specific length. Hope you like this post. Feel free to ask questions related to this Dart random string generation function. I’d be happy to answer.


To conclude this tutorial, hope you now have a detailed practical knowledge of how to implement Dart random string generation function. I’ll be very happy to receive your feedback on this post.

I’d also welcome you to visit my other tutorials on Flutter app development and Python programming as well. Thank you for reading this post.

Leave a Comment

Your email address will not be published.