Flutter Provider Tutorial

This took me a while to figure out, so let me keep my notes here for posterity’s sake.

1/ include the package

import 'package:provider/provider.dart';

2/ have a central file where you govern how the provider works

class YourModel {
  String? address;
  YourModel({
    required this.address
  });
}

class ProviderManagingYourModel extends ChangeNotifier {
  List<YourModel> _list = [];

  List<YourModel> get list => _list;

  void add(YourModel p) {
    _list.add(p);
    notifyListeners();
  }
}

3/ include that file and when you use it it’ll look like this

// likely happens inside a setState

   YourModel model;
   // fill it up
   Provider.of<ProviderManagingYourModel>(context, listen:false).addModel(model);

4/ that put the information where it needed to go — you’ll access Provider for the info like this:

// you'll be using it like

   List<YourModel> pp = Provider.of<ProviderManagingYourModel>(context).list;

5/ in your main runApp it’ll be attached to the argument

runApp(
  ChangeNotifierProvider(
    create: (context) => ProviderManagingYourModel(),
    child:
      MaterialApp( ...

5/ the Provider system holds onto the “type” of the managing object, and if you have more than one of these kinds of managed objects that wants to propagate changes in the Widget tree, then you have to specify multiple providers slightly differently