0

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...'),
    );
  }

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

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.