2

Hello im new in dart language, is someone can help me to write this condition better, it works but it's a lot repetitive and i feel theres a better ways to write it :

 if (var1 != null || var2 != null || var3 != null || var4 != null || var5 != null){ ShowMyWidget }

thank you.

2
  • Are you sure you want all these variables to be nullable? Commented Dec 28, 2021 at 19:34
  • looks fine to me but if you had more variables you could push them into a Map and then create a map that loops through the Map to check if null for each object in the map Commented Dec 28, 2021 at 19:49

5 Answers 5

4
if (![var1, var2, var3, var4, var5].contains(null)){ ShowMyWidget }
Sign up to request clarification or add additional context in comments.

1 Comment

It hadn't occurred to me before. Thanks, now my codes will be more organized.
2

If you really want, you could do:

if ([var1, var2, var3, var4, var5].any((x) => x != null)) {
  ShowMyWidget();
}

Comments

0

Rather than checking all those variables, I'd make them optional parameters to the widget. If you do that, you can just check if they're null using null-safety inside it, whenever you actually need them.

class TestWidget extends StatelessWidget {
  final String? testPar1;
  final String? testPar2;
  final String? testPar3;
  final String? testPar4;
  const TestWidget({
    this.testPar1,
    this.testPar2,
    this.testPar3,
    this.testPar4,
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(testPar1 ?? 'nope'),
        Text(testPar2 ?? 'nope'),
        Text(testPar3 ?? 'nope'),
        Text(testPar4 ?? 'nope'),
      ],
    );
  }
}

Keep in mind that your way of doing it isn't wrong.

Comments

0

ok thanks you your answers, I'll explain my goal a litle bit more : I have a detail page in may app with many custom widget and i want to check if the variables inside my widgets are null in order to either show or hide it.

My app screenshot

So here for exemple I wanna check if one of the booking link isn't null to display the title.

if (link1 != null || link2 != null || link3 != null || link4 != null || var5 != null){ ShowMyTitle }

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

When testing a lot of variables for non-nullity, it's better to put them into an array and use the nonNulls property:

 if ([var1, var2, var3, var4, var5].nonNulls.isNotEmpty) {
    ShowMyWidget();
  }

Using the nonNulls property, it will also become easy to just process the ones that you need to display:

[link1, link2, link3, link4, link5].nonNulls.forEach((link) => ShowMyWidget(link));

Or shorter, if the parameter of ShowMyWidget is the same type as your links:

[link1, link2, link3, link4, link5].nonNulls.forEach(ShowMyWidget);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.