Hafiz Siddiq
Hafiz Siddiq

Reputation: 699

What is the use of provider in flutter?

Well, I am sort of new to Flutter, My question is why we use providers in Flutter, I know it is used for state management. But I am looking to know the most common use case of providers.

Upvotes: 6

Views: 8142

Answers (4)

Rutvik Moradiya
Rutvik Moradiya

Reputation: 88

A wrapper around InheritedWidget to make them easier to use and more reusable.

By using provider instead of manually writing InheritedWidget, you get:

  • simplified allocation/disposal of resources lazy

  • loading a vastly reduced boilerplate over making a new class every time devtools friendly

  • using Provider, the state of your application will be visible in the Flutter devtools a common way to consume these InheritedWidgets

Upvotes: 1

mewadaarvind
mewadaarvind

Reputation: 399

What is provider and how it's works ?

Provider is a simple way for state management, it works on the concept of PUB_SUB,

Which means there is one provider and multiple subscribers which is called consumers here. ex : youtube.

Whenever there is change, with NotifyChangeListener it will update all the consumers.

Upvotes: 3

SanjaySingh
SanjaySingh

Reputation: 1857

As you asked that why we use provider in flutter, so i will be providing only theoretical explanation which i think will surely help you to understand what actually provider is and why it is used.

Suppose you are working on a large app with a lot of folders and files. Now if the user have interacted with your app(say pressed a button or something like that) then the app have to build itself again to update to the changes that the user had made.But wait! this doesn't looks a good deal, to build the whole app again just to make changes in some particular section.

So, here comes the Provider to solve this problem.

A Provider is basically a container or a storage that stores and provides you with state or data. Also, we know that widgets are arranged in an app like a tree like fashion. so, if u assign Provider to any node in the tree then all the children of that node will have access to the data that is provided by the Provider.

Back the stage ,Provider comes with a listener and these listeners are assigned to the children of the widget to which Provider is attached.

So, If the user interferes with any widget that has a listener then the app will build only that particular (which the user interacted) widget again (not the whole app).

Upvotes: 7

Stephan Thierry
Stephan Thierry

Reputation: 101

You need to be able to move data between your Widgets. It's an easy way to do it.

You start your root Build method in the app with:

@override
  Widget build(BuildContext context) {
    return MultiProvider(  // Multi means you can have more providers if you need
      providers: [
        ChangeNotifierProvider(builder: (context) => MyStateClass()),
      ],
      child: MaterialApp(....

Now you can place all the data you need to share in the MyStateClass() and place underlying Widgets inside:

   Consumer<MyStateClass>(builder: (context, state, child) {

      // your code here - return(SomeOtherWidget());
    })

or inside your Build methods:

  @override
  Widget build(BuildContext context) {
   MyStateClass state = Provider.of<MyStateClass>(context);
   // ... TODO  ... return (Widget)

Upvotes: 4

Related Questions