GithubHelp home page GithubHelp logo

iconnect's Introduction

iconnect

how to use

  1. add package iconnect in pubspec.yaml,then import iconnect in our code files: ` import 'package:iconnect/iconnect.dart';
  2. define a simple class as model:
  class CounterModel {
    int value = 0;
    CounterModel(value) {  
        this.value = value;  
      }  
      increment(int step) {  
        value = value + step;  
        return value;  
      }  
    }
  1. create one or multi instance
   CounterModel _first=CounterModel(0);  
   get first=>register(_first);  
   CounterModel _second=CounterModel(0);  
   get second=>register(_second);  
  1. provider: use it only one times,all model will saved here
        void main() {
          runApp(provider(MyApp()));
        }
  1. connect and listen:if model changed by dispatch,it will rebuild
   Widget showCounter() {
     return connect((context) {
       print('ShowCounter build ');
       listen(context, first);
       return ShowCounterOrigin(value: first.value);
     });
   }
  1. dispatch:all widget listen to model will rebuild
    class ShowButtonOrigin extends StatelessWidget {
      ShowButtonOrigin({Key? key, this.increase}) : super(key: key);
      final VoidCallback? increase;
      @override
      Widget build(BuildContext context) {
        return FloatingActionButton(
          onPressed: increase,
          tooltip: 'Increment',
          child: Icon(Icons.add),
        );
      }
    }

    //it won't be rebuild when dispatch(first),because we have not listen first here
    class ShowButton extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return ShowButtonOrigin(
            increase: () => dispacth(first,() => first.increment(1)));
      }
    }

we also could define model with mixin IConnect

   class CounterModel with IConnect {
     int value = 0;
     CounterModel(value) {
       this.value = value;

       //we could register here
       register();
     }
     increment(int step) {
       value = value + step;
       return value;
     }
   }

then we could

  1. simply create instance
    CounterModel first = new CounterModel(0);
    CounterModel second = new CounterModel(0);
  1. use listen,dispatch,register,unregister in model
    first.listen()
    first.dispatch()
  1. and we don't need to import iconnect any where

what's iconnect?

The simplest and fast library for state mamagement,it only handle the shared model for widgets,it's null-safe, and support for all the pllatform. you don't need those huge framework like provider and redux and flutter hook,they think too many things :D but they solved the simple thing with many many concept and codes. . can create multi instance for a model . when model changed,only widget listen to it will rebuild . only provider and connect function for whole . could define model with mixin IConnect . could use listen,dispatch,register,unregister function,then model is a simply class . if you define a dispose method in model,it will run when unregister.

six function and zero concept,that's connect

we only connect models to widget tree all the api of connect is six simple functiuon ,that's all: provider/connect register/unregister dispatch/listen

provider is a function to init the app,so all model could work with widget tree.

you just use it one times in an app.

define model for itself,without any rule,we only connect it with widget tree

it's not based on type,you define a model only for the itself.the model is a class you just create,without any rule,with out base class or interface. we use model as instance,not for type,so we could simplely add multi instance for same model type. you could simple use register and unregister,to add or remove model.you could use registed model as a class instance in your widget,that's mean widget will not rerender.you should listen to a registed model,when it dispatch changes event,your widget will rerender.

design ui only for itself,without any rule,you don't need to know connect

connect function return a widget,you must provide a function with build function,you could use model or listen to model there.

without any concept about async model,we should only do single things here for models

it could worked with future and stream in your models,listen and dispatch could update the ui simply.

get starter

you could find the usage in example folder,it's a simple counter app,it's a demo for: . how to define a model and register it . how to dispatch and listen . how to work with future and AsyncSnapshot,and handle errors . how to work with stream and update ui.

iconnect's People

Contributors

by90 avatar

Stargazers

 avatar

Watchers

 avatar

iconnect's Issues

实现基于id的持久化

通过fromjson和tojson处理,注意null问题
暂时考虑使用sqflite?或者drift?或者hype?实际上服务端也是一种方式。

将model改为bloc

不再使用内置的asyncSnapshot,而是使用futureBuilder和streamBuilder
这需要修改model和counter实例
这里是手工bloc的教程,包括窗体验证、登录、比较密码、购物车等场景,比较老的。
https://juejin.cn/post/6844903812063297544
注意我们不要引入实践概念,ui到bloc只是dispatch即可。

https://www.raywenderlich.com/31973428-getting-started-with-the-bloc-pattern
这里也是手工处理,其中提到了每次刷新bloc重新载入一遍的问题,它使用rxdart解决。
它是简单的文章列表。

判断model是否相等

如果model重写了相等逻辑,则:
即使dispatch,若model没有改变,也不需要重新build
这里亦可考虑,监听部分内容,但我们既然用于处理了不同model,对监听者就无法进一步细化。

这样的好处是进一步提高性能

监听改用bloc的方式

目前是简单的使用provider.of
可考虑使用bloc方式监听?
作为核心功能的listen和dispatch,需要仔细的权衡。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.