Making non-nullable class fields not required if they have a default value

Andre Pena

I have the following class:

class MarkdownRenderingContextData {
  MarkdownRenderingContextData({
    required this.textSize,
    required this.textStyle
  });
  MarkdownTextSize textSize = MarkdownTextSize.text;
  MarkdownTextStyle textStyle = MarkdownTextStyle.normal;
}

What I want is:

  • If you don't pass textStize and textStyle, they have the default value.
  • If you pass them, they must not be null.

I'm not being able to do it. Dart is forcing me to add the required in the construtor, which is making them mandatory during instanciation and I don't want it. If I make them NOT required (e.g MarkdownTextSize?) now I have to check for null during use.

Is there any way around it?

Peter Koltai

You can specify a default value in the constructor, and make the members final:

class MarkdownRenderingContextData {
  MarkdownRenderingContextData({
    this.textSize = MarkdownTextSize.text,
    this.textStyle =  MarkdownTextStyle.normal
  });
  final MarkdownTextSize textSize;
  final MarkdownTextStyle textStyle;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Default value for Required fields in Entity Framework migrations?

Why does the default value of an empty list have 2 empty index when making class list

Entity Framework Core: default value when migrating nullable column to required

Non-nullable reference types' default values VS non-nullable value types' default values

Why Kotlin data classes can have nulls in non-nullable fields with Gson?

General way of picking nullable properties and making them non-nullable

Extending a class and making the propeties required

"Nullable object must have a value" exception after checking for null on a non-primitive/non-struct object

Calculating summation of input fields have default value attribute by javascript

Swashbuckle: Make non-nullable properties required

How to merge two objects of a class with nullable fields, keeping the non-null values?

Adding custom fields to Bills and Adjustment screen causes "nullable object must have a value"

nullable object must have a value

Flutter SharedPreferences non-nullable instance fields

SQL Server: why is it possible to set default NULL value on NON-NULLable column?

Static inner class fields value (default 0, why?)

moshi nullable list fails with "required value"

Non nullable types by default in HotChocolate.AspNetCore

Non-nullable default return null warning

django: default for non-nullable field

Non-nullable by default: how to enable the experiment?

error in Custome class with required and not required fields

How to deal with the required value which does not have a default value in Django model

"An object reference is required for the non-static field, method, or property" for nested classes accessing parent class fields

Default value for fields

Default value for blank fields

Making a singleton have all static fields

Is the value of `null` allowed for fields that are required?

Make multiple Formik-fields required with Yup if any of them have a value