skip to Main Content

i’ m trying to display notification who has random strings who are stored in a List, everything is working but the problem is when i press allow to display notifications, it only show the same string from the list is not updating as i want to show another string that previous one. Here is the code:

String? randomYou;
    String? randomName;
    Color? randomColor;
    final double height = MediaQuery.of(context).size.height;
    final double width = MediaQuery.of(context).size.width;
    final random = new Random();
    randomName = names[random.nextInt(names.length)];
    randomYou = random1[random.nextInt(random1.length)];

 onPressed: () {
                          showToast();
                          NotificationService()
                              .showNotification(1, 'Hello', randomName!);
                        },

I had tried with setState but it s not updating

2

Answers


  1. You should regenerate the random string

    1. Create a function to generate the random strings
    generateStrings() {
      final random = new Random();
      randomName = names[random.nextInt(names.length)];
      randomYou = random1[random.nextInt(random1.length)];
    }
    
    1. Call the function before you show the notification
    onPressed: () {
       generateStrings(); // Regenerate the string each time the button is pressed
       showToast();
       NotificationService()
         .showNotification(1, 'Hello', randomName!);
    },
    
    Login or Signup to reply.
  2. Can you try to push a function that generates random string? Like:

    String RandomGenerator(){
       return "YourRandomString";
    }
    

    and

    onPressed: () {
                      showToast();
                      NotificationService()
                     .showNotification(1, 'Hello', RandomGenerator();
                    },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search