为什么我们需要使用ChangeNotifierProvider而不仅仅使用ChangeNotifier?

huangapple go评论62阅读模式
英文:

Why do we need ChangeNotifierProvider instead of just using a ChangeNotifier?

问题

I am trying to learn how to use flutter_riverpod for state management, I am having trouble understanding what the "need" for ChangeNotifierProvider is , same thing for StateNotifierProvider. Let's take the following ChangeNotifier for example:

class CounterNotifier extends ChangeNotifier {
  int value = 0;

  void increment() {
    value++;
    notifyListeners();
  }
}

Why is it "advised" to declare a ChangeNotifierProvider for my ChangeNotifier and use it to watch() and read() the value of my ChangeNotifier like so:

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final counterProvider = ChangeNotifierProvider((ref) => CounterNotifier());

class ChangeCounterPage extends ConsumerWidget {
  const ChangeCounterPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          ref.read(counterProvider).increment();
        },
        child: const Icon(Icons.add),
      ),
      body: Center(
        child: Text(ref.watch(counterProvider).value.toString()),
      ),
    );
  }
}

Why not just use the ChangeNotifier (like we do with provider)? In the official documentation of Riverpod, it says: "we are using the ChangeNotifierProvider to allow the UI to interact with our ChangeNotifier class." -> what does that mean? Is ChangeNotifierProvider a widget that wraps around the ChangeNotifier and thus makes it "visible" in the widget tree? I've also read that it makes the "manipulation" of our state easier or more manageable, but I can't understand why or how it is needed.

英文:

I am trying to learn how to use flutter_riverpod for state management, I am having trouble understanding what the "need" for ChangeNotifierProvider is , same thing for StateNotifierProvider. Let's take the following ChangeNotifier for example :

class CounterNotifier extends ChangeNotifier {
  int value = 0;

  void increment() {
    value++;
    notifyListeners();
  }
}

Why is it "advised" to declare a ChangeNotifierProvider for my ChangeNotifier and use it to watch() and read() the value of my ChangeNotifier like so:

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final counterProvider = ChangeNotifierProvider((ref) => CounterNotifier());


class ChangeCounterPage extends ConsumerWidget {
  const ChangeCounterPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
        
          ref.read(counterProvider).increment();
        },
        child: const Icon(Icons.add),
      ),
      body: Center(
       
        child: Text(ref.watch(counterProvider).value.toString()),
      ),
    );
  }
}

Why not just use the ChangeNotifier (like we do with provider) ? in the official documentation of riverpod it says : " we are using the ChangeNotifierProvider to allow the UI to interact with our ChangeNotifier class." -> what does that mean ? is ChangeNotifierProvider a widget that wraps around the ChangeNotifier and thus makes is "visible" in the widget tree ? I've also read that it makes the "manipulation" of our state easier or more manageable but I can't understand why or how it is needed.

答案1

得分: 2

A ChangeNotifier必须被监听,并与变更事件关联的操作,以便发挥其作用。在监听器消失之前,它也必须停止监听。

ChangeNotifierProvider将监听ChangeNotifier的过程封装成一个"ProviderListener"协议,该协议知道如何在事件发生时使观察提供者的小部件重建,并知道在小部件被释放时如何断开连接。

英文:

A ChangeNotifier must be listened, with an action associated with change events, in order to be useful. It also must be unlistened before the listener goes away.

A ChangeNotifierProvider wraps the process of listening to a ChangeNotifier into a "ProviderListener" protocol, which knows how to make the widget watching the provider rebuild when events occur, and how to disconnect itself when the widget is disposed.

huangapple
  • 本文由 发表于 2023年6月9日 06:45:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定