flutter in the state management Provider

 

  Today flutter briefly about the state management, we use the provider;

  ps: Let me talk about a concept, Model, model, and there we are ready to define the global use of data or methods;

  For chestnut: We have a User class, used to store information about the user, such as After logging in, we will get some personal user data, then these data can be written in the Model as properties, while our internal User, also upUser provides a method for updating user information, then this method can also be written in the Model, OK, that 'we are ready user Model;

  Here is the question, go, go, go

  

  A, Provider of three brothers:

    Boss - MultiProvider

    Second child - Providers

    老三 -- Provider.of<T>(context)  /  Widget Consumer

  

  Two, three brothers of a portfolio:

    In fact well understood, we do not say essence, first talk about boxing, boxing relying go try to figure out the essence, will be made easier;

    1, Boss MultiProvider 

  Build the Widget (BuildContext context) {
     return MultiProvider ( 
      Providers: [], /// This is not considered a 
      Child: MaterialApp ( 
        title: 'Provider Demo' , 
        initialRoute: '/' , 
      ), ); 
  }
    

  The code above presumably everyone is familiar with, a root widget (which is not considered an providers: []), the boss's task is to wrap the root, we will be ready to establish contact Model and View; can be imagined as a giant octopus wrapped a tree, he can be arbitrary tentacles stretch a section of tree branches;

  

  2, the second stage Providers:

    In fact, well understood, since the boss can we prepared various Model transported to each node, provided that it is not the boss must know what are the Model it needs to be sent away, her second child appeared, he was in charge of pre-defined, It needs to be shared in the Model;

    That second child how to define it, above us "is not considered" that there was a relation, providers, the second is an array, providers of s obviously a very complex, and is used with his boss, the second is to supply , the boss is the supplier, the consumer is the youngest;

    Of course, this array inside the second child would not be so simple, providers: [provider, provider, providers], it certainly will not be this way, because we use different Model of demand, the second son of the array installed there are several, Let's say it, the second son of several commonly used:

    Son : provider does not need to be monitored, and some constants or methods do not really "affect the situation as a whole", that is to say they will not be asked to change and change with such a demand is defined by the eldest son;

    Two sons : ChangeNotifierProvider father to son is more and more requirements, it will be with some of the data change notification is updated, that is to say, for example, the Model is used in multiple page, which then when one is changed he should tell the other places, change update, so demand is defined by two sons;

    Three sons : ChangeNotifierProxyProvider this son asks even higher, so its name long length than the second child, the second child as he not only like to inform the update, the update will also coordinate between the Model and the Model, such as a ModelA dependent on another ModelB, ModelB update, he will let ModelA depend on it also will be updated, and this is the youngest of the living;

    The specific wording of it, is also very simple, the official website to go from ;

 

  3, the third person a little strange

    We said above youngest consumers, that is to say, he is responsible for removing data from each node to use;

    Why blame him, because he has two forms, one is required to bind with the display in the widget, the other not, just as the need to use data;

    Turned a

 var foo = Provider.of<T>(context);

    T is that you need a Model, just so you get out on the foo;

    Turned two

Consumer<T>(
    builder: (context,foo, child) => Text('$foo.text')
)

    widget display, foo above, is removed from the Model data, free to use;

 

    OK, three brothers basic introductions, also today I just saw Provider, if not understand, you can also pretend to be, ha ha ha, kidding, we hope to make progress together;

 

    

  

Guess you like

Origin www.cnblogs.com/webcabana/p/12149972.html