Widget inheritance versus composition - what should I do?
After a bit of research, I learned that Flutter for the most part prefers to use composition rather than inheritance when it comes to widgets. For example:
- https://flutter.dev/docs/resources/architectural-overview#composition- https://stackoverflow.com/questions/51476234/subclass-a-class-that-extends-statelesswidget-or-statefulwidget-class-https://groups.google.com/g/flutter-dev/c/muVUV4z71fs/m/DS0twymQCAAJ
What if multiple widgets use the same class member (for example an APIService class)? You can create a BasePage and have widget classes inherit from this BasePage widget - but realized this may not be the proper way to do things in Flutter. For example:
Class BasePageWidget {final apiService = APIService();}
Class A extends BasePageWidget {super.apiService to use inherited class}
What is the commonly accepted way to approach this? Do I simply add the class member to all of the widgets the utilize the ApiService? This seems a bit repetitive. For example:
Class A {final ApiService = APIService()...useApiService somwhere in the class}
Class B {final ApiService = APIService()...useApiService somwhere in the class}