ãããã¦èªã¿ãã
FlutterでBLoCだChangeNotifierと振り回されて消耗するまえに - タオルケット体操
çè ã®Flutterã«å¯¾ããå°è±¡ã¯åå¹´åã«ãã®ã¨ã³ããªã¼ãæ¸ããã¨ãããé©ãã»ã©ã«ä½ãå¤ãã£ã¦ããªãã®ã§ãéã«Flutterã¯é常ã«æå¿«ã§ããããããã©ã¤ãã©ãªãªã®ãããããªãã§ããã hachibeechan.hateblo.jp
çè ã®ä¸»å¼µã®äºåã¾ã¨ã
- Reactã®å¦ç¿ã¯å®è³ªFlutterã®äºç¿
- ã¯ã©ã¤ã¢ã³ãã¢ããªãè¨è¨ããã«ããã£ã¦ã¯ActiveRecordãã¿ã¼ã³ã®åçºæããã¦ã¯ãããªã
- çµå±MVX
- RXSteamã¨ã¯ãªãã ã£ãã®ã
- DDDã®åå¼·ãããã¨å¤ãã®ç¤ºåãå¾ããã
- Remi wareãä¿¡ãã
ã¡ãªã¿ã«ãã以ä¸ã§è¿°ã¹ãActiveRecordãã¿ã¼ã³ã¯PoEEAã¨RoRã®ãã®ã®æ··åãããããããã¾ããããå©ç¨ãã¹ããããªãã¨ããç¹ã«ããã¦åä¸ãªã®ã§ç¹ã«åé¡ã¯ããã¾ããã
ã¾ãããµã¼ãã¼ãµã¤ãã§ãã®ãã¿ã¼ã³ãæ¡ç¨ãããã¨ã®å¯å¦ã«ã¤ãã¦ãæ¬ç¨¿ã§è¿°ã¹ããã¨ã¯ãã¾ããã
以ä¸ã«è¶ ããããããå³è§£ãç½®ãã¾ãã
ActiveRecordãã¿ã¼ã³ã®åçºæããã¦ã¯ãããªããã¨ã¯
çè
ã®ã¡ã¤ã³ãã£ã¼ã«ãã¯Reactã«ããWebã®ããã³ãã¨ã³ãã§ã¢ããªéçºããã£ã¤ããã£ãã®ã¯ããåå¹´ãã»ã¼åãã¦ã¿ãããªãããªãã§ãããSPAã®éçºã¯å©ç¨ãã¦ãããã¼ã«ããªãããéãã ãã§ã¢ã¼ããã¯ãã£çã«æ±ããããææ³ã¯ã¢ãã¤ã«ã¢ããªã¨å¤ããã¾ããã
ããã¦ä¸ã®ä¸ã®90%ã®ã¢ããªã¯ããã»ã©å°è¦æ¨¡ãªãã®ã§ããªãéãã¯ãã®ãActiveRecordãã¿ã¼ã³ã®åçºæãã¨ããç½ ããã¤ãããã¯ã«è¸ã¿ã¤ãã¦çæ»ãã¾ãã
ActiveRecordãã¿ã¼ã³ã®åçºæã¨ã¯ãç°¡åã«è¿°ã¹ãã®ã§ããã° Simple app state management - Flutter ã«ããCartModelã®ãããªã¢ããã¼ãã®ãã¨ã§ãã
ãã®ä¾ã«ãããã㪠XXXModel
ã¨ãããããªã¯ã©ã¹ã¯å¤ãã®è¨è¨ã§é »åºãã¾ããããã®ãããªã¯ã©ã¹ã¯ã¢ããªã±ã¼ã·ã§ã³ã®è¦æ¨¡ãæé·ããã«ãããã£ã¦ä»¥ä¸ã®ãããªå½¹å²ãæã¤ãã¨ã«ãªãã¾ã
- ã¢ããªã±ã¼ã·ã§ã³ãæµãããã¼ã¿æ§é ã®è¨è¿°
- ã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ã¸ã®å¯ä½ç¨ï¼ãã¸ãã¹ãã¸ãã¯ã¨å¼ã°ãããã®ã®ä¸é¨ï¼
- èªèº«ãæ½è±¡åãã¦ãããªã½ã¼ã¹ã¨ã®åæï¼Railsã®ActiveRecordã§ããã°RDBã¨äº¤ä¿¡ããããã¯ã©ã¤ã¢ã³ãã§ã¯httpã§éä¿¡ãããã¨ã«ãªãï¼
- èªèº«ãsubscribeãã¦ããViewã¸ã®æ´æ°éç¥
ãâ¦â¦ç§ã®ã¢ãã«ãå½¹å²æã¡ããâ¦â¦ï¼
ã¨ãããã¨ã§ãããªãFatãéçºé²ãã°ã¹ãã²ããã£ãæ¯åº¦ãããã¨ããããã¾ãModelã¯ã©ã¹ã®å®è£
ã§ãããã¾ãã
ãªãWebãã¬ã¼ã ã¯ã¼ã¯çéã§ã¯FatXXXããã½ã¼ã¹ã³ã¼ãã®è¡æ°ãå¤ãç¶æ ãã¨ãã¦æãã¦ãé»éè¡çãªããæ¹ã§è¡æ°ãæ¸ãããã¨ã横è¡ãã¦ããããã¾ãããè¦ãç®ä¸ã³ã¼ããåå²ããã¦ãã¦ã極度ã®å¯çµåãæé»ã®includeå°çã«é¥ã£ã¦ããã°ããã¯Fatã§ããæ°ãä»ãã¾ãããã
ããã«ãã¦ãã®åå¯ãé¿ããã®ã
ã²ã¨ã¤ã®ã¯ã©ã¹ã¯ã²ã¨ã¤ã®å½¹å²ãæã¤ããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã®ååã§ããã
ã¨ããããã§ãã®ãã£ã¤ã®å½¹å²ãããããã¯ã©ã¹ã«å解ãã¦ããã¾ãããã
ãã¼ã¿æ§é ã¯Plain Old Dart âImmutable" ObjectãPODIOï¼Javaã§ããPOJOã§ãï¼ã¨ãã¦å®£è¨ããã
"Immutable" ã¯å®£è¨çUIã«ã¨ã£ã¦éè¦ã§ãã
ã¨ã¯ããEquatableãè¨èªæ¨æºã«çµã¿è¾¼ã¾ãã¦ããSwiftã¨éã£ã¦Dartã¯å ¨é¨æã§ãããªãã¨ãããªãã§ãããFlutterã¯Mutableãã¼ã¹ãªã®ã§æ©æµã¯èãã®ã§ãããçè ã¯Immutableããå¼·ããæ¨å¥¨ãã¾ããçå¤æ§æ¯è¼ãã§ããããã«ãªãã ãã§ããã¸å¤§ããã
Equatableã使ã£ã¦èªåã§ããããfreezedã使ãã®ãã好ããªã»ããé¸ãã§ãã ãããPODIOã¯ãValueObjectã¨ãã¦ã®è²¬åã«éåããªãç¯å²å ã§ããã°æ¯ãèããæã¤ãã¨ãå¯è½ã§ãã
ä¾ãã°firstName
,lastName
ã¨ããå±æ§ãæã¤PODIOããããããåããã¦è¿ãã¦ãããfullName
ã¨ããã¡ã½ãããæã¤ã®ã¯è¨±ããã¾ãã
ãã ãPODIOãèªèº«ã®validityãå¤æãã¦ã¯ããã¾ãããä¾ãã°ã¯ã¦ãªããã°ãæ§ç¯ããã®ã§ããã°ã
Article
ã¨ããPODIOãisValid
ã¨ããã¡ã½ãããæã£ã¦ã¯ãããªãã§ãã
ãªããªãç¾å®ä¸çã®ã¢ããªã«ããã¦ããªãã¼ã·ã§ã³ã¯ãã¼ã¿åä½ã§å®çµãããã¨ã¯ãªãããã§ãã
ä¾ãã°é常ã®æ稿å¦çã«ããã¦ã¯ã¿ã¤ãã«ã¨æ¬æã空ã®è¨äºã¯ä¸æ£ã ã£ãã¨ãã¾ããããããã©ããã®ä¿åããããã¯æ´æ°ã«ããã¦ã¯ãã®éãã§ã¯ãªãã§ããããã¾ããã¹ãã 対çã«é£æé²æ¢ãå ¥ããã®ã§ããã°åå¾ã®ããã°è¨äºã®æ稿ããã®çµéæéãå 容ã®éè¤ããã§ãã¯ããå¿ è¦ãããã¾ãã
ä»ã«ããããã¯ãªã¹ãã«å ¥ã£ã¦ããã¦ã¼ã¶ã¼ã®ã³ã¡ã³ã管çãªã©ãå¿ è¦ã§ãããããããã®ãã¨ãèãã¦ãPODIOã¯ãªãã¹ãç´ æ´ãªå®è£ ã«ã¨ã©ãã¦ããã¾ãããã
ã¡ãªã¿ã«PODIOè¨è¨ã§ããæ©ãã±ã¼ã¹ã¨ãã¦ãåãUser
ã§ãã£ã¦ããAdmin
ãMe
ãªã©ã«ãã£ã¦æã£ã¦ããå±æ§ãç°ãªãâ¦â¦ã¨ãããããªè¨è¨ã«ãªã£ã¦ããã¢ããªãããããããã¾ããã
ãã®å ´åãå¤ãã®äººã¯ç¶æ¿ã使ã£ã¦è§£æ±ºããããããã¾ããããä»ç¾å¨Dartã«ã¯Kotlinã§ããsealed class
ã«ãããæ©è½ããªããããæ´¾çå ã®ä¿è¨¼ãè¡ãããå¤æ´ã«å¼±ãã§ãã
ä»æ¹ããªãã®ã§ freezed | Dart Package ã®Unionæ©è½ã使ã£ã¦è§£æ±ºãã¾ããããã¢ããªã±ã¼ã·ã§ã³ã®ç¶æ ã¸ã®å¯ä½ç¨ã¯ "Behavior" ã¨ããååã§å®è£ ãã¾ãã
DDDãåå¼·ãããã°ããå 容ãé ããæãåºããããã¯ãã½è¤éãªã®ã¯ãã®é¨åã¨çæ¯ã«åãåã£ã¦ããããã§ãã
ãã ãé£ãããã¨ãããã¨çµç¹çã¹ã±ã¼ã©ããªãã£ã®ç¢ºä¿ãé£ãããªãã¾ãããä½ããè¨è¨ããæ¬äººãç解ã§ããªããªãã®ã§ããã¾ããããããã§éè¦ãªã®ã¯
- Behaviorã¯ç¶æ ãæããªãï¼ã©ãã§ä½åã¤ã³ã¹ã¿ã³ã¹åãããããã¢ããªã¯å£ããªãï¼
- ã¢ããªã±ã¼ã·ã§ã³ã¸ã®å¯ä½ç¨ã¯ãã¹ã¦Behaviorã«è¨è¿°ããã
- Behaviorã¯ã¢ããªã®ã¦ã¼ã¹ã±ã¼ã¹ã®æ½è±¡åã§ãããã¨è¨ãæãããã¨ãã§ãã
- Storeï¼ä»¥ä¸ã§èª¬æãã¾ãï¼ãå¤æ´ãã¦ããã®ã¯Behaviorã ã
- Viewã¯ã·ããªãªã«å¿ãã¦Behaviorãå¼ã³åºãã ã
- DIã¨ãããã¾ã使ã£ã¦ãã¹ã¿ããªãã£ã«ã¯æ°ãä»ãã¦ã
ãã®ååãå®ããã¨ã§ãã
ããããå®ãã°å¾ã¯å²ã¨èªç±ã«æ¸ãã¦ãªãã±ã¼ã§ãã
å ·ä½çãªå®è£ æ¹æ³ã«ã¤ãã¦ã¯ FlutterでBLoCだChangeNotifierと振り回されて消耗するまえに - タオルケット体操 ã¨ããåèã«ãªãã¨ãããã¾ãã
å種ã¡ã½ããã¯Storeã¸ã®å¯ä½ç¨ã主ãªç®çã¨ããã®ã§åºæ¬çã«è¿ãå¤ã¯voidã¨ãªãã¾ãããéåæå¦çã®çµäºãæå¦ãéç¥ããããã«Futureãè¿ãã¦ãããã§ããå種ãªã½ã¼ã¹ã¨Storeã®åæ㯠"Repository" ã使ã£ã¦è¡ãã¾ãã
åçååã¨ããè¿°ã¹å§ããã¨ã¾ãè²ã ã¨ããã®ã§ããã以ä¸ã®ååãå®ãã°æ¥µç«¯ã«ç ´ç¶»ãããã¨ã¯ããã¾ãã- Rest, GraphQL, LocalStorageãªã©ã®ã¢ã¯ã»ã¹ã¯ãã¹ã¦Repositoryãçµç±ãã¦è¡ã
- ãã©ã³ã¶ã¯ã·ã§ã³ãªã©ã®è¤éãªè¦ä»¶ã«ããæ½è±¡åã®æ¼ãã¯ããç¨åº¦è¨±å®¹ãã
- ãç¶æ
ã®æ½è±¡åãã§ã¯ãªããããªã½ã¼ã¹ã®æ½è±¡åãã§ãããã¨ãæèããã
ä¾ãã°DBã®ãªã½ã¼ã¹ããRepositoryã®CRUD IFãèªåã§çæãã¨ãwwwwã¿ãããªã®ã¯å ¸åçãªè² ããã¿ã¼ã³ã
ã¯ã¦ãªããã°ã®ã¢ããªãä½ãã¨ãã¦ãè¨äºã®ä¸èº«ãã¨ã£ã¦ããã¯ã¨ãªãã¯ã¦ã¼ã¹ã±ã¼ã¹ãããã©ã¼ãã³ã¹ãã¥ã¼ãã³ã°ãªã©ã®è¦ä»¶ããæ°ãã¿ã¼ã³ãã£ã¦ããããããªããä½åº¦ã§ãæ¸ããDBã«å¯¾ããCRUDæä½ã¿ãããªçºæ³ã«ã¨ãããã¦ããã¨ã¯ã©ã¤ã¢ã³ãã¢ããªã®è¨è¨ã¯ã¹ã±ã¼ã«ããªããã¨ãããDBã®ãã¨ã¯æå³çã«å¿ããã¹ã
Viewã¸ã®æ´æ°éç¥ã¯Observableã使ã£ã¦è¡ãã¾ããStoreã¨å¼ã³ã¾ãã
å ·ä½çã«ã©ã®ã©ã¤ãã©ãªã使ããã¯å人好ã¿ã¨ããããã¨ãããã®ã§å¥½ããªãã®ã使ãã¾ãããã
ChangeNotifierã使ããã人ã¯çè ã®ã¨ã³ããªã«ããModelProtocol
ï¼ãã®è¨äºãæ¸ããã¨ãã¯ã¾ã Modelã®éåã«éãæ¹ããã¦ããï¼ãåèã«ããããããªãæ¹ã¯ state_notifier | Dart Package ã使ãã¾ãããã
Flutterã®InheritedWidget
ã«ä¹ããé½åä¸ãStoreã¯åä¸contextä¸ã«ããã¦ã¯Singletonã§ããã¾ããã¾ããç§ã®ããã°ãstate_notifierãèªãã°ãããã¾ãããèªèº«ã¯Stateã®å¤æ´ãæ¤ç¥ãã¦Viewã¸ã¨éç¥ããããã«mutableã¨ãªãã¾ãããstateã¯immutableã§ãã
ããã¯Flutterã®ããæ¹ã¨ç¸åããã¨ããããããã¾ãããããªã¢ã¯ãã£ããªã¢ã¼ããã¯ãã£ã«mutableãæã¡è¾¼ãã§ããã¯ãªãã¨ã«ã¯ãªããªãã®ã§å¼·ãæ°æã¡ã§ããæãã¾ãããã
Storeã¯ãµãã¾ããæã¡ã¾ããã
ããããæ¤è¨ãã¾ããããæãããªãã»ããããã§ããå
¨é¨Behaviorã§ãã£ã¦ãã ããã
ã¡ãªã¿ã«çéã§ã¯ãªã¯ã³ã³æ±ãããã¦ä¹
ããBLoCã§ãããããã¯ViewModelã®ãã¿ã¼ã³ãªã®ã§ããããããã§ã¯æ¯è¼ã®å¯¾è±¡ã«ãªãã¾ããã
ä¸ã®å³ã§ããã㦠Widget
ã§ã¯ãªã View
ã¨åä»ãã¦ããã®ã¯ãã®è¾ºã®ä¼ç·ã§ãã
ããã¾ã§ã®ã¾ã¨ã
Flutterçã®Shougoãã¨Remiæ°ãä½æãã¦ããã©ã¤ãã©ãªã®ã³ã³ã»ãããç解ããããã«ã¯Reactãããå¿ è¦ããããéã«Reactçµé¨è ãFlutterããã£ããå½¼ã¨åããã®ãå 製ãã¦ãã¾ãã
ç°¡åãªååãå®ãã°è¤éãªã¢ã¼ããã¯ãã£ã¯ãããªãã
ãã ãã¹ã±ã¼ã«ããã¯ã©ã¤ã¢ã³ãã¢ããªã®è¨è¨ãããã®ã§ããã°CRUDãåºæºã«ãã "Model" ã¨ããã¯ã©ã¹ã¯ä½ãã¹ãã§ã¯ãªã*1ã
Modelã¯ã¢ã¸ã¥ã¼ã«ã®ç·ä½ã¨ãã¦æµ®ãã³ä¸ãããã®ã§ããã
é·ããªããããªã®ã§ä»åã¯ããã¾ã§ã§ãã
次以éã¯Storeè¨è¨ã®ãã¹ããã©ã¯ãã£ã¹ãBehaviorã®å
·ä½ä¾ãBLoC亡ãç¾ä»£ã®Viewãã¸ã«ã«ãµãã¤ãã«è¡ãè¦ããã§ãããFlutterã®GraphQLäºæ
ãªã©ã«ã¤ãã¦æ¸ãã¦ããããã¨ãããã¾ãã
ãã¨ãã¤ã©ã¼ãã¬ã¼ãã¿ãããªå
·ä½ä¾ãä½ã£ãã»ãããããããããããããªãã§ããã
ã¤ã¥ã
Storeç·¨æ¸ãã¾ããã
*1:ããããUtilsã¯ã©ã¹ãé¿ããããã«Modelã¯ã©ã¹ãé¿ãã