Reputation: 567
In Flutter, is there any advantage/disadvantage to returning 'Widget' rather than the exact type from a method inside a stateless/stateful widget?
Example
Container buildContainer() {
return Container(
child: Text('Something...'),
);
}
Widget buildContainer2() {
return Container(
child: Text('Something...'),
);
}
Upvotes: 0
Views: 376
Reputation: 27137
i don't think this is a matter of concern. it totally depends on you what you want to return.
1) If you return Container:
In this case your most parent widget must be container, it gives surety of parent widget, which is container. However, if you want to change parent widget then you have to change return type also.
2) If return type is Widget.
In this case it gives freedom of return type widget. your parent widget can be anything, but when you want specific type of widget as parent that time this way don't give you surety.
Upvotes: 1