What is the use of provider in flutter?

2,522

Solution 1

Provider is a wrapper around InheritedWidget to make them easier to use and more reusable. this is probably the approach you should start with.

The provider package is easy to understand and it doesn’t use much code. It also uses concepts that are applicable in every other approach.

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

  • simplified allocation/disposal of resources
  • lazy-loading
  • a largely reduced boilerplate over making a new class every time
  • devtools friendly
  • a common way to consume these InheritedWidgets
  • increased scalability for classes with a listening mechanism that grows exponentially in complexity (such as ChangeNotifier, which is O(N²) for dispatching notifications).

See : Usage

This answer is given on the basis of : provider

Solution 2

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)

Solution 3

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).

Solution 4

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.

Share:
2,522
Hafiz Siddiq
Author by

Hafiz Siddiq

Updated on December 14, 2022

Comments

  • Hafiz Siddiq
    Hafiz Siddiq over 1 year

    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.