i have a model it looks like this
class UserModel {
final String? username;
final int? age;
final String? phoneNumber;
UserModel(
{required this.age, required this.phoneNumber, required this.username});
@override
String toString() {
return 'Username => $usernamenAge => $agenPhone number => $phoneNumber';
}
}
what im confusing for now is what is better to use with ? or without ? like final String? username
or final String username
and if some how the data i get from server username
is null how do i handle it ? i want to keep the ui code clean as possible so i dont want to put if(user.username == null)
in the ui i want to keep it in the model or controller how ?
2
Answers
In Dart, the use of
?
after a type (e.g.,String?
) indicates that the variable can be nullable, meaning it can either hold a non-null value of that type or benull
. On the other hand, if you declare a variable without?
(e.g.,String
), it means the variable must always contain a non-null value of that type.In your
UserModel
class, you have defined the fields as nullable:This means that these fields can be assigned
null
values. Whether you should use nullable or non-nullable types depends on the requirements of your application and how you want to handle null values. Here are some considerations:Use nullable types (
String?
,int?
, etc.) when:Use non-nullable types (
String
,int
, etc.) when:Regarding your second question about how to handle null values in the model or controller without cluttering the UI code, you can achieve this by providing default values or transforming the data when constructing the
UserModel
object. Here’s an example of how you can handle null values in the model constructor:In this modified
UserModel
, if any of the provided values (username, age, or phoneNumber) arenull
, it will default to a specified value (e.g., ‘Default Username’ for username) instead of allowing null values. This way, you ensure that yourUserModel
always contains non-null values, which can make it easier to work with in the UI code.I will suggest to not include
?
operator on class property. Now, it is not mandatory/forced to follow, but, from the perspective to reduce null exception, your code must be null safe, alternatively, you should use?
based on your need.In my experience, having a class like this, is a good-to-have structure.
You can ask, what if my
phoneNumber
is notrequired
to create aUserModel
, how will I manage that. In that case, let’s**remove**
therequried
keyword fromconstructor
ofthis.phoneNumber
and assign adefault value
, thedefault value
will only assign, if and only if, you didn’t provide anyphoneNumber
as argument, while creating anUserModel
object.another way to rewrite the same thing is
For server data handling, I will recommend to use a factory constructor while maintaining the above structure.
To summarize the answer, you should avoid using
nullable
varialble most of the time. That doesn’t imply that you shouldn’t usenullable
data type at all. What if you want to check ifphoneNumber
is provided or not by checking if the value isnull
or not. I mean who check the data is provided or not by checking if it is empty or not (acctualy I do).