Search packages, repositories and app categories

Package detailsv5.1.0

scope

A Dependency injection (Inversion of Control) library. Supports nested Scopes and multiple values of the same type.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
254K
Likes
14
Pub points
150

Install package

flutter pub add scope

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

Scope is a dependency injection (Inversion of Control) library for Flutter and Dart that enables structured, hierarchical dependency management. It supports nested scopes for isolating dependencies and allows multiple instances of the same type, making it ideal for modular applications. With clean API design and minimal boilerplate, Scope simplifies state management and service instantiation across different parts of an app. It works seamlessly across all major platforms including Android, iOS, Web, macOS, Windows, and Linux.

Use cases

  • Modular application architecture
  • Testing with isolated dependencies
  • Managing shared and scoped services
  • Multi-instance service handling
  • Cross-platform app development

Key features

  • Nested scopes support
  • Multiple values of the same type
  • Minimal boilerplate
  • Platform-agnostic
  • Clean API design
  • Easy integration with existing code

Suitable for

  • Large-scale Flutter apps
  • Testable architectures
  • Microservices-like app modules
  • Teams needing dependency isolation
  • Developers avoiding complex DI frameworks

Considerations

  • Learning curve for new users
  • Limited documentation compared to mature alternatives
  • Best suited for apps with complex dependency trees

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

Scope

Scope provides Inversion of Control using the dependency injection (DI) pattern for Dart applications.

Scope allows you to inject values into a scope and then 'use' those dependencies from any method (or constructor) called within that scope.

Scope is incredibly easy to use with no additional plumbing required.

Scope is not a replacement for the likes of Provider. Provider does dependency injection for your BuildContext whilst Scope provides DI for your call stack.

For Java developers Scope provides similar functionality to thread local variables.

Scope supports nested scopes; overload or add additional values at each level.

Supporters

Scope is supported by OnePub the private Dart package repository.

https://onepub.dev

The best way to understand Scope is with an example:

import 'package:scope/scope.dart';
import 'package:money2/money2.dart';

const ageKey = ScopeKey<int>();
const heightKey = ScopeKey<int>();
const incomeKey = ScopeKey<Money>();
const countKey = ScopeKey<int>();

void main() async {
    /// create a Scope
    final scope = Scope()
    
    /// inject values
    ..value<int>(ageKey, 18)
    ..value<int>(heightKey, 182) // centimetres
    /// single value from factory method
    ..single<Money>(incomeKey, () => calcIncome)
    /// sequence of values from factory method.
    ..sequence<int>(countKey, () => tracker);
    
    /// run some code within the Scope
    await scope.run(() async => a();
}
void a() {
    print('Your height is ${use(heightKey)}');
    printRealWorth();
}

/// `use` the injected values
void printRealWorth() {
    print('You are the ${use(countKey)} person to ask');
    print('You are ${use(ageKey)} years old');
    print('You earn ${use(incomeKey} per hour.');
}

Money calcIncome() {
    final age = use(ageKey);
    if (age < 18) return Money.parse(r'$10.00');

    return Money.parse(r'$20.00');
}

var count = 0;
int tracker()
{
    return count++;
}

See the Scope documentation at:

https://scope.onepub.dev

The best practice guide is worth a read:

https://scope.onepub.dev/guides/best-practice

Authors: 
Philipp Schiffmann <philippschiffmann93@gmail.com>
S. Brett Sutton

Scope is a reimagining of Philipp's zone_id package. All credit goes to Phillipp's original implementation without which Scope wouldn't exist.

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package