Flutter: Global Access vs Scoped Access with Provider

Andrea Bizzotto
Jun 10 · 5 min read
  • A HomePage, used to sign out the user
  • A LandingPage, used to decide which page to show
  • we can’t easily swap FirebaseAuth with a different authentication service if we want to.

Global Access vs Scoped Access

There is one main problem with global access. And we can use our example code to illustrate this:

Scoped Access

Scoped access is about making objects available to an entire widget sub-tree.

Enter Provider

This library by Remi Rousselet and the Flutter team makes life easier.

// pubspec.yaml
dependencies:
provider: ^3.0.0
  • Use Provider.of<FirebaseAuth>(context) where needed, instead of FirebaseAuth.instance.

Wrap up

In this article we have learned about scoped access as an alternative to global access.

References

For more articles and video tutorials, check out Coding With Flutter.

Coding with Flutter

Learn to build iOS and Android apps with Dart and Flutter

Andrea Bizzotto

Written by

iOS, Flutter Developer & Blogger ❖ https://codingwithflutter.com ❖ http://bizz84.github.io ❖ Open Source https://github.com/bizz84 ❖ Watching #ClimateChange

Coding with Flutter

Learn to build iOS and Android apps with Dart and Flutter