skip to Main Content

i have tow lists and i’m making shuffle to get a random element but when i combine the tow shuffle values i get this error :

This expression has a type of ‘void’ so its value can’t be used.
(Documentation) Try checking to see if you’re using the correct API;
there might be a function or call that returns void you didn’t expect.
Also check type parameters and variables which might also be void.

code:

final randomEmoji = emojis.shuffle();
final randomlLast = last.shuffle();

final finalText = randomlLast + randomEmoji;

2

Answers


  1. Chosen as BEST ANSWER

    fixed just call last.shuffle(); , don't assign it to a new variable


  2. The shuffle method does not return a new list. It shuffles the original list in place and returns void hence the error you are getting.

    If you want to get a random element from each list, you can do something like this:

    import 'dart:math';
    
    final random = Random();
    
    final randomFromEmoji = emojis[random.nextInt(emojis.length)];
    final randomFromLast = last[random.nextInt(last.length)];
    
    final finalText = '$randomFromLast$randomFromEmoji';
    

    If you just want to concantenate shuffled version of both list, do the below:

    final random = Random();
    emojis.shuffle(random);
    last.shuffle(random);
    
    final finalList = last + emojis;
    

    Where finalList is the combined shuffle list.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search