How can I check to see if provided class exists

Viewed 742

I am using Provider package in my flutter app.

I am conditionally providing a ChangeNotifier class using provider.

A subsequent build method down the tree wants to check to see if the class exists, and it is not a failure case if it doesn't.

How can I do this using Provider package. Currently when I call...

Provider.of<MyChangeNotifier>(context);

It throws if MyChangeNotifier does not exist up the tree. All I want is a quick and simple check for MyChangeNotifier existence, because it is an expected case.

1 Answers

As @Scorb mentioned, calling Provider.of<MyChangeNotifier?>(context) with null safety enabled will try to obtain the matching provider and if none are found null will be returned instead of throwing.

Related