1

Can someone give me idea how provider notify the state?

I don't want to use ChangeNotifierProvider, Can you give me a suggestion without library?

I just need better explanation with example. How provider combine InheritedWidget.

Raiyan Safir
  • 23
  • 1
  • 5

3 Answers3

2

What do you think about the following example (inspired by an answer here) with an AnimatedBuilder:

import 'package:flutter/material.dart';

class MyChangeNotifier extends ChangeNotifier {
  int count = 0;

  void addOne() {
    count++;
    notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  final MyChangeNotifier myChangeNotifier = MyChangeNotifier();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: ExampleButton(myChangeNotifier),
        ),
        floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.add),
          onPressed: myChangeNotifier.addOne,
        ),
      ),
    );
  }
}

class ExampleButton extends StatelessWidget {
  final MyChangeNotifier myChangeNotifier;
  const ExampleButton(this.myChangeNotifier, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
        animation: myChangeNotifier,
        builder: (context, child) {
          return OutlinedButton(
              onPressed: myChangeNotifier.addOne,
              child: Text(
                'Tap me - or the floating button\n\n${myChangeNotifier.count}',
                textAlign: TextAlign.center,
              ));
        });
  }
}

void main() => runApp(MyApp());

The ChangeNotifier implements the Listenable class. You can see here how to listen to that Listenable, for example with an AnimatedBuilder (what my code does). A ChangeNotifyProvider (I know, you don't want that) would also implement that for you and notify your widgets lower in the widget tree about changes.

Stephan
  • 399
  • 1
  • 9
0

Here is some idea for you :

widgets listen to changes and notify each other if there is a rebuild. As soon as the state changes, that particular widget rebuilds without affecting other widgets in the tree.

Three major components make all of this possible: the ChangeNotifier class in Flutter, the ChangeNotifierProvider (primarily used in our sample app), and the Consumer widgets.

Whatever change in the state observed from the ChangeNotifier class causes the listening widget to rebuild. The Provider package offers different types of providers – listed below are some of them:

The Provider class takes a value and exposes it, regardless of the value type ListenableProvider is the specific provider used for listenable objects. It will listen, then ask widgets depending on it and affected by the state change to rebuild any time the listener is called ChangeNotifierProvider is similar to ListenableProvider but for ChangeNotifier objects, and calls ChangeNotifier.dispose automatically when needed ValueListenableProvider listens to a ValueListenable and exposes the value

StreamProvider listens to a stream, exposes the latest value emitted, and asks widgets dependent on the stream to rebuild FutureProvider takes a Future class and updates the widgets depending on it when the future is completed

0

As a suggestion to learn provider from this article-

https://medium.com/flutter-community/making-sense-all-of-those-flutter-providers-e842e18f45dd

Vishal_VE
  • 1,852
  • 1
  • 6
  • 9