Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function not creating Set #1

Open
Entrebear opened this issue Sep 28, 2022 · 3 comments
Open

Function not creating Set #1

Entrebear opened this issue Sep 28, 2022 · 3 comments

Comments

@Entrebear
Copy link

Entrebear commented Sep 28, 2022

I am trying to use the randomMultipleWeightedChoice function to get a weighted random element as a result, from a Map of Strings and integers. However it does not seem to be creating the resulting Set. I am using the latest version of Flutter and package. When I try print the resulting set it doesn't show anything.

Code

String categorygen(Map<String, num> tempcatmap) {
  final tempset = randomMultipleWeightedChoice(tempcatmap, 1, null);
  print(tempset);
  final tempstring = tempset.elementAt(0);
  print(tempstring);
  return tempstring;
}

Map that I'm passing in (result of print of the Map)
{cat1: 4, cat2: 2, cat3: 2, cat4: 3}

Error from console

Bad state: No element

The relevant error-causing widget was: 
  Container Container:file:///C:/Apps/ /lib/pages/game.dart:145:18
When the exception was thrown, this was the stack: 
#0      Iterable.reduce (dart:core/iterable.dart:353:7)
#1      randomMultipleWeightedChoice (package:multiple_random_choice/multiple_random_choice.dart:25:67)
#2      categorygen (package: /models/game_mechanics.dart:47:19)
@MenEnger
Copy link
Owner

MenEnger commented May 3, 2023

@Entrebear
I apologize for the delay in seeing your comment. I will now check on this issue.

@MenEnger MenEnger closed this as completed May 3, 2023
@MenEnger
Copy link
Owner

MenEnger commented May 3, 2023

It was accidentally closed and has been reopened.

@MenEnger MenEnger reopened this May 3, 2023
@MenEnger
Copy link
Owner

MenEnger commented May 3, 2023

@Entrebear
I tried to run the following code in my environment (Dart v2.17.6).
It appears to be working correctly.
I would like to test it with the latest Dart version at this time.

void debug1() {
  print('==debug1==');
  final Map<String, num> tempcatmap = {
    'cat1': 1,
    'cat2': 2,
    'cat3': 2,
    'cat4': 3
  };
  final tempset = randomMultipleWeightedChoice(tempcatmap, 1, null);
  print(tempset);
  final tempstring = tempset.elementAt(0);
  print(tempstring);
}

Result:

==debug1==
{cat3}
cat3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants