Motivation
Why do you need Modddels ?
Let's say you want to model a Student
object. The Student
has a name
, an age
, and an email
. You may create your class/data-class this way :
You will then need to validate the name
, age
and email
in various parts of your app. The Student
model will be used in different places : for example, a widget that displays a student profile, or a function addStudent(Student newStudent)
, etc...
There are several problems with this approach :
Where should you validate the
name
,age
andemail
?After validation is done, how can you distinguish between a valid
Student
instance and an invalid one ?How can you ensure that valid
Student
instances are passed to functions or widgets that require them, and likewise, invalid instances are passed to those that specifically handle them?How to handle an invalid
Student
differently based on what field is invalid and why it's invalid, all this in various parts of the app ?If you have, for example, a widget that displays the
email
of aStudent
. How can you prevent any random string from being passed to the widget ?
All these problems (and more) can be resolved by using this package.
The Modddels package offers a way to validate your models and deal with its different states (valid, invalid...) in a type-safe and compile-safe way.
This package is NOT a data-class generator. It is meant to create models that are at the core of your app (If you use DDD or Clean architecture, those would be in the "domain" layer). Therefore, you are meant to create separate classes for things like json serialization, either manually or with tools like freezed and json_serializable (These classes are usually called "DataTransferObjects", "DTOs", or simply "models").
Last updated