How to generate random ascii strings in Flutter

Last updated Nov 12, 2021

Random number or Random Strings generators are useful for many different purposes. Aside from obvious applications like generating random numbers for the purposes of creating unpredictable results in a computer game.

So, In today's tutorial, we will see how we can create random ascii strings in Flutter. Let's start

Step 1: Create a new Flutter Application.

Step 2: Add a line like this to your package's pubspec.yaml

dependencies:
  random_string: ^2.3.1

Once added you will get access to functions like:

  • randomNumeric : Creates random numbers

  • randomBetween: Creates random number between two numbers.

  • randomString: Creates random String.

  • randomAlpha: Create random alpha numeric. 

Step 3: Now were you want to get the random string you can use function randomString in Text widget and once you run the application you will get random string

for example: 

Text(

                randomString(20),

                style: TextStyle(fontSize: 30),

              ),

 

 

Complete Source Code to to generate random ascii numbers in flutter

import 'package:flutter/material.dart';

import 'package:random_string/random_string.dart';

 

void main() => runApp(RandomApp());

 

class RandomApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      debugShowCheckedModeBanner: false,

      home: DemoPage(),

    );

  }

}

 

class DemoPage extends StatefulWidget {

  @override

  _DemoPageState createState() => _DemoPageState();

}

 

class _DemoPageState extends State {

  TextStyle text = TextStyle(fontSize: 30);

  int number = 25;

 

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      backgroundColor: Colors.blueGrey,

      appBar: AppBar(

        title: Text('Random Everything'),

      ),

      body: Container(

        decoration: BoxDecoration(

          shape: BoxShape.circle,

          color: Colors.grey.shade200,

        ),

        child: Center(

          child: Column(

            mainAxisAlignment: MainAxisAlignment.center,

            children: [

              Text(

                randomNumeric(10),

                style: text,

              ),

              Text(

                randomBetween(10, 100).toString(),

                style: text,

              ),

              Text(

                'This is Constant $number Number',

                style: text,

              ),

              Text(

                randomString(20),

                style: text,

              ),

              Text(

                randomAlphaNumeric(8),

                style: text,

              ),

              Text(

                randomMerge('Hello', 'Friends'),

                style: text,

              ),

              Text(

                randomAlpha(5),

                style: text,

              ),

            ],

          ),

        ),

      ),

    );

  }

}

 

 

Output:

 

Video Tutorial

 

Conclusion: In this tutorial, we have learned how we can generate random string, random number, random acsii numbers in Flutter.

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

638 Views