Flutter Random Word Generator
Flutter Random Word Generator

Hi Guys, Welcome to Proto Coders Point, In this Flutter Article let’s checkout how to generate a random word in flutter dart. first by picking a randon word from a given list and second exampe by using a word_generator flutter library.

Generate random nouns example in flutter dart

Below is an dart program example to generate random nouns:

import 'dart:math';

void main() {
  List<String> nouns = [
    'Balloon',
    'Car',
    'Horse',
    'Raincoat',
    'Train',
    'Monkey',
    'Window',
    'Rocket',
    'Iron',
    'Girl'
  ];
  
  Random random = Random();
  int randomIndex = random.nextInt(nouns.length);
  String randomNoun = nouns[randomIndex];
  
  print(randomNoun);
}

In above Program we have a list of noun words created, Then we make use of Random class from math package that will generate a random number from 0 to the length of the list, and thus us use this random number and retrieve a noun list index and print it on console.


Flutter dart program to generate a random word

In above program we saw that we created a list of word and randomly pick a word from the list, but suppose you want to generate a random word without predefined list of words.

Here is one way by by which we can generate random nouns or words without using pre-defined words list is by using flutter word generator library.

First add the dependency in your flutter project

open pubspec.yaml file and under dependencies section add:

dependencies:
  word_generator:

Generate Random Nouns in flutter dart

import 'package:word_generator/word_generator.dart';

void main() {
  String randomNoun = WordGenerator().randomNoun()
  print(randomNoun);
}

Generate Random Verbs in flutter dart

 void main() {
        String randomNoun = WordGenerator().randomVerb()
        print(randomNoun);
 }

Generate Random Sentence using word generator

void main() {
        String randomNoun = WordGenerator().randomSentence();
        print(randomNoun);
 }

Generate Random Names in flutter dart

void main() {
        String randomNoun = WordGenerator().randomName()
        print(randomNoun);
}

Generate Random usernames in dart

The below Code will generate a random name using word generator package, We need to make the username in such a ways that it dont contain space and should be in lowercase.

String randomNoun = WordGenerator().randomName();
String username = randomNoun.toLowerCase().replaceAll(' ', '');
print(username);