OOP

  • Although the framework recommends the use of functional programming, it also provides object-oriented programming support.
    • ViewPart
      • Need to override the ‘build’ function.
      • The required state, dispatch, and viewService parameters have become fields of the object and can be used directly.
      • It is immutable, so there should be no need to define variable fields internally.
    • EffectPart
      • Need to override the ‘createMap’ function.
      • The required Context has been flattened as the fields which can be used directly.
      • Fields can be defined and their visibility is limited to themselves.
      • It must be used with higherEffect.
  • Sample Code
  1. class MessageView extends ViewPart<MessageState> {
  2. @override
  3. Widget build() {
  4. return Column(children: [
  5. viewService.buildComponent('profile'),
  6. InkWell(
  7. child: Text('$message'),
  8. onTap: () => dispatch(const Action('onShare')),
  9. ),
  10. ]);
  11. }
  12. }
  13. class MessageEffect extends EffectPart<MessageState> {
  14. ///we could put some Non-UI fields here.
  15. @override
  16. Map<Object, OnAction> createMap() {
  17. return <Object, OnAction>{
  18. Lifecycle.initState: _initState,
  19. 'onShare': _onShare,
  20. };
  21. }
  22. void _initState(Action action) {
  23. //do something on initState
  24. }
  25. void _onShare(Action action) async {
  26. //do something on onShare
  27. await Future<void>.delayed(Duration(milliseconds: 1000));
  28. dispatch(const Action('shared'));
  29. }
  30. }
  31. class MessageComponent extends Component<MessageState> {
  32. MessageComponent(): super(
  33. view: MessageView().asView(),
  34. higherEffect: higherEffect(() => MessageEffect()),
  35. );
  36. }