Flutter Generate Random Number Between Min and Max Range Dart

Hello friends, In today’s tutorial we would learn about random number. In flutter we would generate random number using their own function Random(). This function would return us random number from given maximum range. But using a simple tweak we can use Random method to generate random number within a range. We can define minimum and maximum range of random number. So when we generate random number the number would be between maximum and minimum range. So let’s get started 🙂 .

Contents in this project Flutter Get Generate Random Number Between Min and Max Range:-

1. Open your project’s main.dart and import material.dart and dart:math package.

2. Creating our void main runApp() method and here we would call our main MyApp class.

3. Creating our main MyApp extends StatelessWidget class. This is our main Class.

4. Creating a function named as randomNumber(). In this function we would first make the object of Random() class. Now we would make 2 integer type variable named as min and max. In the min number we would define the minimum number from which we want to start the random number and max number. Now at the last we would print the generate positive random number within a range on Terminal window.

5. Creating Widget Build area -> Material App -> Scaffold Widget -> Center Widget -> Column Widget -> Here we would make our Raised Button. On the onPress event of Button we would call the randomNumber() function.

6. Complete source code for main.dart file:

Screenshots:-

Flutter Generate Random Number Between Min and Max Range Dart

Leave a Reply

Your email address will not be published. Required fields are marked *