Skip to content

How to Watch Optional Property #8

Closed
@cliftonlabrum

Description

I have an optional selectedFlight property in a class like this:

class ModelFlight with ChangeNotifier {
  //+++
  static ModelFlight get to => di<ModelFlight>();
  //+++

  Flight? _selectedFlight;
  Flight? get selectedFlight => _selectedFlight;
  set selectedFlight(Flight? value) {
    _selectedFlight = value;

    notifyListeners();
  }
}

...but it crashes when I set up the property value watcher in a widget:

Widget build(BuildContext context) {
  watchPropertyValue((ModelFlight model) => model.selectedFlight); //<-- !! Exception !!
}

_TypeError (Null check operator used on a null value)

How can I watch a nullable property that won't be set until later?

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