Skip to content

mrmitew/provider

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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<Model>(
      // we voluntary reuse the previous value
      valueBuilder: (_, old) =>  old ?? Model(),
      // child: ...,
    );
  }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 100.0%