RiverPod 筆記

1、

extension on AutoDisposeRef {
// When invoked keeps your provider alive for [duration]
void cacheFor(Duration duration) {
final link = keepAlive();
final timer = Timer(duration, () => link.close());
ref.onDispose(() => timer.cancel());
}
}

final myProvider = Provider.autoDispose<int>((ref) {
ref.cacheFor(Duration(minutes: 5));
});

2峭拘、 New in Riverpod 2.0: NotifierProvider and AsyncNotifierProvider

https://github.com/bizz84/async_notifier_example_riverpod.git

@riverpod --> family
https://codewithandrea.com/articles/flutter-riverpod-generator/

3婆跑、 Disadvantage: Not all provider types are supported yet

Out of the six different kinds of providers, riverpod_generator only supports two of them:

  1. Provider (works with generator)
  2. StateProvider (legacy)
  3. StateNotifierProvider (legacy)
  4. FutureProvider (works with generator)
  5. StreamProvider
  6. ChangeNotifierProvider (legacy)

As we will see in the next article, StateProvider and StateNotifierProvider can be replaced by the new Notifier and AsyncNotifier.

But for now, there isn't a straightforward migration strategy that will work for existing apps. And if your app uses a realtime database and relies heavily on streams, you can't take advantage of the new syntax yet.

4、When to use ref.watch vs ref.read?

As a rule of thumb, we should:

call ref.watch(provider) to observe a provider's state in the build method and rebuild a widget when it changes
call ref.read(provider) to read a provider's state just once (this can be useful in initState or other lifecycle methods)

However, in the code above we have called ref.read(provider.notifier) and used it to modify its state.

The .notifier syntax is available with StateProvider and StateNotifierProvider only and works as follows:

call ref.read(provider.notifier) on a StateProvider<T> to return the underlying StateController<T> that we can use to modify the state
call ref.read(provider.notifier) on a StateNotifierProvider<T> to return the underlying StateNotifier<T> so we can call methods on it

Alongside ref.read and ref.watch, we also have ref.listen

5魏割、## Scoping Providers

With Riverpod, we can scope providers so that they behave differently for a specific part of the application.

這里列舉了一個(gè) ListView 性能優(yōu)化的列子

// 1. Declare a Provider
final currentProductIndex = Provider<int>((_) => throw UnimplementedError());

class ProductList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(itemBuilder: (context, index) {
      // 2. Add a parent ProviderScope
      return ProviderScope(
        overrides: [
          // 3. Add a dependency override on the index
          currentProductIndex.overrideWithValue(index),
        ],
        // 4. return a **const** ProductItem with no constructor arguments
        child: const ProductItem(),
      );
    });
  }
}

class ProductItem extends ConsumerWidget {
  const ProductItem({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // 5. Access the index via WidgetRef
    final index = ref.watch(currentProductIndex);
    // do something with the index
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末譬嚣,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子钞它,更是在濱河造成了極大的恐慌拜银,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,539評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件遭垛,死亡現(xiàn)場(chǎng)離奇詭異尼桶,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)锯仪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門(mén)泵督,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人庶喜,你說(shuō)我怎么就攤上這事小腊。” “怎么了久窟?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,871評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵秩冈,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我瘸羡,道長(zhǎng)漩仙,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,963評(píng)論 1 295
  • 正文 為了忘掉前任犹赖,我火速辦了婚禮队他,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘峻村。我一直安慰自己麸折,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評(píng)論 6 393
  • 文/花漫 我一把揭開(kāi)白布粘昨。 她就那樣靜靜地躺著垢啼,像睡著了一般窜锯。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上芭析,一...
    開(kāi)封第一講書(shū)人閱讀 51,763評(píng)論 1 307
  • 那天锚扎,我揣著相機(jī)與錄音,去河邊找鬼馁启。 笑死驾孔,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的惯疙。 我是一名探鬼主播翠勉,決...
    沈念sama閱讀 40,468評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼霉颠!你這毒婦竟也來(lái)了对碌?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蒿偎,失蹤者是張志新(化名)和其女友劉穎朽们,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體酥郭,經(jīng)...
    沈念sama閱讀 45,850評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡华坦,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了不从。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惜姐。...
    茶點(diǎn)故事閱讀 40,144評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖椿息,靈堂內(nèi)的尸體忽然破棺而出歹袁,到底是詐尸還是另有隱情,我是刑警寧澤寝优,帶...
    沈念sama閱讀 35,823評(píng)論 5 346
  • 正文 年R本政府宣布条舔,位于F島的核電站,受9級(jí)特大地震影響乏矾,放射性物質(zhì)發(fā)生泄漏孟抗。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評(píng)論 3 331
  • 文/蒙蒙 一钻心、第九天 我趴在偏房一處隱蔽的房頂上張望凄硼。 院中可真熱鬧,春花似錦捷沸、人聲如沸摊沉。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,026評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)说墨。三九已至骏全,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間尼斧,已是汗流浹背姜贡。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,150評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留棺棵,地道東北人鲁豪。 一個(gè)月前我還...
    沈念sama閱讀 48,415評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像律秃,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子治唤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容