vendredi 29 janvier 2021

Does Flutter has the possibility to use only a if condition inside the build method?

I worked with flutter for almost a year and since it wasn't production-ready for web development I needed to switch to react.

To render object conditionally inside my build method I had to put an if-else statement rendering my custom widget if the condition was met or a random empty container in the other case.

@override
Widget build(BuildContext context) {
 return Container(
   child: condition == true 
       ? new MyCustomWidget() 
       : new Container() //Random empty container to fill the else statement
 )
)

Even if this doesn't cause any rendering problem, it's still can be ambiguous for someone reading the code. After working with react I saw that it provides a way to display only the if block using the following notation:

<div>
  {condition == true && <MyCustomReactComponent/>} //this block is displayed only if the condition is met
</div>

I was guessing if flutter has a way to do something similar.

Aucun commentaire:

Enregistrer un commentaire