Search code examples
flutterdartdart-null-safetyflutter2.0

Does it make sense to have required null parameter?


We can define a parameter that is required and nullable in Flutter version 2, does it make sense? Why should we have a required parameter which accepts null?


Solution

  • In terms of database values like in SQL, NULL specifically refers to a "missing" value. In other words it references a value that could or will be defined but has not (yet) been.

    To answer your question, it depends on what the field is and whether or not it's being stored in any kind of state, whether that state is front or back end.

    One example where I would consider using a nullable but required field would be if I'm soft deleting records and am marking a deleted_at column. I want to require this field for a soft delete, but it is expected to not be defined until an actual delete occurs, whenever that is.

    Flutter is basically a data-driven UI that should have one UI page for any given state. So if you, say, had a user record that was soft deletable on the backend and a profile user page that might be shown (to an admin or whatever), you might want to set the deleted_at field in the Dart code to be required but nullable to distinguish the state of a soft deletable user record.