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:
textStize
and textStyle
, they have the default value.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?
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.
Comments