Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to use provider package

How to use provider package

makicamel

July 16, 2019
Tweet

More Decks by makicamel

Other Decks in Programming

Transcript

  1. provider ? •scoped modelʹۙ͘ɺػೳΛ૿΍ͨ͠ͷ͕provider
 Google I/O 2019Ͱprovider͕Googleެࣜਪ঑ʹ 
 ͍·ผͷํ๏Λ࢖͍ͬͯΔਓ͸
 ແཧʹม͑ͳ͍͍ͯ͘Αʂ

    ৽࢝͘͠ΊΔͳΒ
 provider͕அવΦεεϝ✨ 1SBHNBUJD4UBUF.BOBHFNFOUJO'MVUUFS (PPHMF*0 
 IUUQZPVUVCFE@NDTNSG* ."5546--*7"/
  2. •ୡ੒͢΂͖໨త(ग़ྗ)Λࣔ͠
 ࣮ݱ͢Δखଓ͖͸γεςϜʹ ೚ͤΔ •ཉ͍͠σʔλ
 Λهड़ •ॲཧํ๏͸໰Θͳ͍
 એݴܕ / ໋ྩܕ •໨తΛୡ੒͢ΔͨΊͷํ๏

    Λʮखଓ͖ʯͱͯࣔ͢͠
 •ॲཧɾऔಘํ๏
 Λهड़ •࣮ߦ͢΂໋͖ྩྻͰ
 ߏ੒͞ΕΔ IUUQTKBXJLJQFEJBPSHXJLJએݴܕϓϩάϥϛϯά
 IUUQTKBXJLJQFEJBPSHXJLJ໋ྩܕϓϩάϥϛϯά ͪͳΈʹ3VCZ͸
 ΦϒδΣΫτࢦ޲Λಋೖ໋ͨ͠ྩܕݴޠ How What
  3. class SomeWidget extends StatefulWidget { SomeWidgetState state; SomeItem value; @override

    createState() => state = SomeWidgetState(); void updateWidget() => state.updateState(); } class SomeWidgetState extends State<SomeWidget> { void updateState() { setState(() {}); } @override Widget build(BuildContext context) { return SomeAwesomeWidget(...); } } એݴܕ / ໋ྩܕ
  4. class AnotherWidget extends StatelessWidget { @override Widget build(BuildContext context) {

    return FlatButton( child: Text('PUSH'), onPressed: () { var widget = SomeWidget() ..value = someItem ..updateWidget(); }, ); } } એݴܕ / ໋ྩܕ
  5. void someHandler() { final items = somehowGetMyItems(); items.add(myAwesomeItem); } class

    SomeWidget extends StatelessWidget { @override Widget build(BuildContext context) { var items = somehowGetMyItems(context); return SomeAwesomeWidget(...); } } એݴܕ / ໋ྩܕ providerͷग़൪ UIͱ stateͷ෼཭
  6. Model •OrderΫϥε
 Item(ྉཧ)΍
 ςʔϒϧ൪߸Λϝϯόʹ࣋ͭ •OrderPoolΫϥε
 Φʔμʔ͕ೖΔͱ૿͑ɺ
 Φʔμʔ͕ఏڙ͞ΕΔͱݮΔ ΦϜϨπ ×1
 εʔϓ

    ×1
 A୎ ΦϜϨπ ×1
 εʔϓ ×1
 A୎ ΤϏϑϥΠ ×2
 ϥΠε ×2
 B୎ ϋϜΧπ ×1
 ΤϏϑϥΠ ×2
 C୎
  7. class Order { int id; Table tableNum; List<Item> items; Order(this.id,

    this.tableNum, this.items); } Model ΦϜϨπ ×1
 εʔϓ ×1
 A୎
  8. class OrderPool { int _max = 0; List<Order> _orders =

    []; List<Order> get orders => _orders; void add(Table tableNum, List names) { var items = names.map( (name) => Item(name)).toList(); _orders.add(Order(_max, tableNum, items)); _max += 1; } void remove(...) {...} } Model ΦϜϨπ ×1
 εʔϓ ×1
 A୎ ΤϏϑϥΠ ×2
 ϥΠε ×2
 B୎ ϋϜΧπ ×1
 ΤϏϑϥΠ ×2
 C୎
  9. class OrderPool extends ChangeNotifier { int _max = 0; List<Order>

    _orders = []; List<Order> get orders => _orders; void add(Table tableNum, List names) { var items = names.map( (name) => Item(name)).toList(); _orders.add(Order(_max, tableNum, items)); _max += 1; notifyListeners(); } void remove(...) {...} } ChangeNotifier มߋͨ͠Αͬͯ௨஌
  10. class OrderApp extends StatelessWidget { @override Widget build(BuildContext context) {

    return ChangeNotifierProvider( builder: (context) => OrderPool(), child: MaterialApp( title: 'OrderManagement', home: OrderBoard(), )); } } ChangeNotifierProvider ࢖͏Ϋϥε(OrderBoard)ͷ্ҐͰએݴ ChangeNotifier͋ Λܧঝͨ͠
 ModelΛࢦఆ
  11. class OrderBoard extends StatelessWidget { @override Widget build(BuildContext context) {

    return Scaffold( appBar: AppBar(title: Text('OrderBoard')), body: Consumer<OrderPool>(builder: (context, orderList, _) { return ListView(children: [ for (Order order in orderList.orders) ... [OrderCard(order: order)] ]); }), ); } } Consumer ΦϜϨπ ×1
 εʔϓ ×1
 A୎ ΤϏϑϥΠ ×2
 ϥΠε ×2
 B୎ ϋϜΧπ ×1
 ΤϏϑϥΠ ×2
 C୎
  12. •௕Ί͚ͩͲͨͿΜΘ͔Γ΍͍͢BLoCύλʔϯͷղઆ | @kabochapo - Qiita
 https://qiita.com/kabochapo/items/8738223894fb74f952d3 •FlutterͷBLoC(Business Logic Component)ͷϥΠϑαΠΫϧΛ
 ਖ਼֬ʹ؅ཧͯ͠ఏڙ͢Δbloc_providerύοέʔδͷղઆ

    | mono  in Flutter ' - Medium
 https://medium.com/flutter-jp/bloc-provider-70e869b11b2f •[Flutter] package:provider ͷ֤ϓϩόΠμͷৄࡉ | @kabochapo - Qiita
 https://qiita.com/kabochapo/items/a90d8438243c27e2f6d9 •provider | rrousselGit/provider - GitHub
 https://github.com/rrousselGit/provider •provider_shopper | filiph/samples - GitHub
 https://github.com/filiph/samples/tree/provider-shopper/provider_shopper •Pragmatic State Management in Flutter (Google I/O'19)
 http://youtu.be/d_m5csmrf7I ࢀߟ