provider 1.0.0 copy "provider: ^1.0.0" to clipboard
provider: ^1.0.0 copied to clipboard

outdated

An helper to easily exposes a value using InheritedWidget without having to write one.

Build Status pub package

An helper to easily exposes a value using InheritedWidget without having to write one.

This is especially useful with patterns such as BLoC or when storing our state inside InheritedWidget. As having to manually write a StatefulWidget AND an InheritedWidget can be tedious.

Usage #

Provider usage:

import 'package:provider/provider.dart';

main() {
  runApp(
    Provider(
      value: 42,
      child: Builder(
        builder: (context) => {
          // explicitly pass the generic type
          final value = Provider.of<int>(context);

          // type inference works too:
          int value2;
          value2 = Provider.of(context);

          // do something with it
        }
      )
    )
  );
}

StatefulProvider usage

class Model {}

class Stateless extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StatefulProvider(
      // we voluntary reuse the previous value
      valueBuilder: (Model old) =>  old ?? Model(),
      // child: ...,
    );
  }
}
10.4k
likes
0
points
3.56M
downloads

Publisher

verified publisherdash-overflow.net

Weekly Downloads

An helper to easily exposes a value using InheritedWidget without having to write one.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on provider