Home > Enterprise >  How to check the boolean values and request from api if values true?
How to check the boolean values and request from api if values true?

Time:04-19

I want to request jokes from api. However, if user checks a checkbox, I want to block those jokes.

If I want to blacklist some of them, I can use this:

https://v2.jokeapi.dev/joke/Any?blacklistFlags=something,something,something

Here is my request function:

 Future<void> getJokes(
    bool isNsfw,
    bool isReligious,
    bool isPolitical,
    bool isRacist,
    bool isSexist,
    bool isExplicit,
  ) async {
    
  }

I want to know how can I check the boolean variables one by one and if any of it true, add this url's end.

https://v2.jokeapi.dev/joke/Any?blacklistFlags=

CodePudding user response:

I would use this approach. Rename the keys to whatever you like the flags to be

Future<void> getJokes(
    bool isNsfw,
    bool isReligious,
    bool isPolitical,
    bool isRacist,
    bool isSexist,
    bool isExplicit,
    ) async {
  var flags = {
    'nsfw': isNsfw,
    'religious': isReligious,
    'political': isPolitical,
    'racist': isRacist,
    'sexist': isSexist,
    'explicit': isExplicit
  };
  flags.removeWhere((key, value) => value == false);
  var flagsString = flags.keys.join(',');
  if (flagsString.isNotEmpty) flagsString = '?blacklistFlags='   flagsString;

  //then always append this flagsString to the url
}

CodePudding user response:

here is the link to the documentation

https://jokeapi.dev/

create variables or check box with the following values Programming,Miscellaneous,Dark,Pun,Spooky,Christmas when a user check the correct box include it in the api like below example

https://v2.jokeapi.dev/joke/Miscellaneous,Dark

  • Related