How is Scope different from 'x'

We receive a lot of questions about how Scope is different from other packages in the Dart ecosystem. So here are a few comparisons.

If one of these is your package and I've mis-represented it, apologies in advance and drop me a line with any corrections.

Get It

GetIt is quite different in that it is essentially a singleton.

There is only one instance of getit for you whole application that you can add services into.

When using Scope you will end up with lots of instances of Scope and each one may have different values injected.

As Dart is asynchronous you may have multiple Scopes active at one time (even before you start nesting scopes).

A classic example is a call to a database (db) connection.

From a FutureBuilder you might do a db call.

So you get a db from your pool and push it into a scope.

Now any code called within that scope has access to that db connection.

Now this is where Scope is different to getIt

If you have another FutureBuilder running at the same time it may also need to access the db.

So you create another scope with its own db connection.

The two future builders now run happily, each with their on db connection and they won't interfere with each other.

Usage of Scope is complementary to GetIt and many apps will use both.

Provider

Provider and similar packages (river pod) also provide dependency injection but they do it for you widget build method.

In Flutter widget build methods are not on your call stack. Instead the build method is called from the Flutter framework.

This is one reason why Flutter has been so problematic as its hard to pass data around. You can't just pass things down your call stack.

Scope on the other hand does dependency injection for you call stack. It works for any methods/functions/constructors that are called on your call stack.

Usage of Scope is complementary to GetIt and many apps will use both.

Last updated