I have a multilevel user app in which I have implemented role based widget. Such as;
final user=Provider.of<UserNotifier>(context).getRole(); //returns role string
(user=="Admin")?SomeWidget():Container(),
There is another option such as:
Visibility (
visible: (user=="Admin"),
child: SomeWidget()
),
Which would be the best approach among this two?
I would say, first option(following) is best option.
(user=="Admin")?SomeWidget():Container(),
As can be seen from code it only build widget which is required. For Example, if condition is true then it will build SomeWidget otherwise it build Container.
Main benefit id that it will improve app performance, it will not build SomeWidget if it is not require. Suppose your SomeWidget is really big widget in which you are getting data from server and all that, in this case it will continue doing all that think even that widget is not displaying. So we can say that all the request we are doing is not worthy.
While in Second case, it will keep doing all that thing that's why i think that way is not good.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With