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

667

Solution 1

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
}

Solution 2

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

Share:
667
themmfa
Author by

themmfa

Updated on January 04, 2023

Comments

  • themmfa
    themmfa over 1 year

    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=
    
    • Ivo
      Ivo almost 2 years
      What are you exactly having problems with? What have you tried? A simple if else could do it or not?
    • themmfa
      themmfa almost 2 years
      I don't think it can solve the problem. At least it would be complicated. Cuz I have 6 different variables and I have to check all. Or am I missing something? @IvoBeckers
    • themmfa
      themmfa almost 2 years
      I mean I can pass 1, or 3 or 5 or none.
    • krumpli
      krumpli almost 2 years
      Since the URL is a string just concatenate to the end of the URL. Iterate over your flags and add accordingly. Surely this should be straight forward unless i misunderstood something?
    • themmfa
      themmfa almost 2 years
      @krumpli Yeah you understood correctly. However, I don't know how ı can iterate thorough them? Should add them in a list?
    • krumpli
      krumpli almost 2 years
      Yeah just add them to a list something like [all your bools here].forEach((e) => {// if bool true: add to url}));
    • themmfa
      themmfa almost 2 years
      @krumpli i think your this is what I was looking for.Thanks a lot :)
  • themmfa
    themmfa almost 2 years
    this solved my problem too. Thank you so much :)