Skip to content

Nullable Strings in UI: Prefer Non-Null with Defaults #17

@ajaxspace

Description

@ajaxspace

Problem:
UI variables such as helperText are declared as nullable (String?), even though the absence of a value can be represented with an empty string:

final String? helperText;

Issues:

  • Requires use of null-aware operators (??, !, ?.) in every usage
  • Adds unnecessary null-safety complexity for a simple use case
  • Can lead to runtime exceptions if the ! operator is misused

Recommendation:
Prefer using non-nullable String with a default empty value ('') for optional UI fields like labels, helper text, captions, etc.

✅ Suggested fix:

final String helperText;

const MyWidget({this.helperText = ''});

Usage becomes safe and concise:

Text(helperText); // no need for null checks

Impact:

  • Cleaner widget code
  • Eliminates unnecessary null-checks
  • Reduces risk of runtime null errors
  • Promotes consistency in widget APIs

Use nullable only when null has distinct meaning from '', which is rarely the case in UI text.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions