Jane Doe
Pro Plan
Strongly typed languages like Dart help us to prevent bugs by demanding we provide necessary parameters when we initialize a new instance of a class.
However this an be cumbersome in a few situations.
Imagine you have a list view screen for articles. Articles come with title, subtitle, caption, date and possibly more on this screen.
However on the detailed view page(viewing an individual article) the articles come with more fields(comments[], related_articles[], etc)
If we want to reuse our class definition for the resource(along with the many other CLEAN Architecture pillars) we'd find ourselves in a difficult situation.
? throughout our code and makes it less elegant/maintainable.class Article { final String id; final String title; // ...other list fields Article({required this.id, required this.title});} class ArticleDetail extends Article { final String body; final List<String> tags; // ...other detail fields ArticleDetail({ required String id, required String title, required this.body, required this.tags, }) : super(id: id, title: title);}Personally, I find that option 2 is the best.
Use a Base Class + Extension/Detail Class
Use Composition (Optional)
Use Factory Constructors for Parsing
? fields make code harder to read and maintain.Nullable fields feel like an anti-pattern in strongly typed languages. It's best to avoid them altogether by using a composition approach.