ChangeNotifierProvider vs ChangeNotifierProvider.value

后端 未结 4 1923
悲&欢浪女
悲&欢浪女 2020-12-17 15:24

I am quite new to this framework and working on state management using provider package where I come across ChangeNotifierProvider and ChangeNotifierProvi

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 16:02

    Is an important difference between ChangeNotifierProvider.value and with the create function. When you're using Provider in a single list or grid item, Flatter removes items when they leave the screen and re adds them when they reentered the screen in such situations what actually happens is that the widget itself is reused by Flutter and just the data that's attached to it changes. So Flatter recycles the same widget it doesn't destroy it and recreate it. when we are using Provider with the create function.

    ChangeNotifierProvider(
      create: (_) => new MyChangeNotifier(),
      child: ...
    )
    

    ☝☝☝ here which is content changes over time and our provider won't pick us up.

    In a single list or grid item, we should use Provider dot value.

    ChangeNotifierProvider.value(
      value: new MyChangeNotifier(),
      child: ...
    )
    

提交回复
热议问题