简体   繁体   English

如何使用 flutter riverpod statenotifier、ref watch 和 asyncvalue 获取加载状态/微调器?

[英]How do I get loading state/spinner with flutter riverpod statenotifier, ref watch and asyncvalue?

I have the below example which does refresh the data but does not give a spinner as expected.我有下面的示例,它确实刷新了数据,但没有按预期提供微调器。 I tried using ref.listen to switch to async loading, i tried await in front of the call even though it didn't make sense.我尝试使用 ref.listen 切换到异步加载,我尝试在调用前等待,即使它没有意义。

I tried ref.refresh, ref.invalidate... Expected behaviour : Open app, see spinner, tap button, see spinner again before text changes.我尝试了 ref.refresh、ref.invalidate ...预期行为:打开应用程序,查看微调器,点击按钮,在文本更改之前再次查看微调器。 Actual behaviour : Open app, see spinner, tap button, text changes and no spinner shows.实际行为:打开应用程序,查看微调器,点击按钮,文本更改并且没有微调器显示。 also see https://github.com/neiljaywarner/riverpod_watching_dep_no_spinner_sample I'm using flutter 3.0.5 and hooks_riverpod: ^2.0.0-dev.9.另请参阅https://github.com/neiljaywarner/riverpod_watching_dep_no_spinner_sample我正在使用 flutter 3.0.5 和 hooks_riverpod:^2.0.0-dev.9。

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

void main() => runApp(ProviderScope(child: MyApp()));

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) => MaterialApp(title: 'Riverpod Spinner Demo',
      theme: ThemeData(primarySwatch: Colors.blue), home: MyHomePage());
}

class MyHomePage extends ConsumerWidget {

  @override
  Widget build(BuildContext context, WidgetRef ref) => Scaffold(
      appBar: AppBar(title: const Text('Riverpod Spinner Question'),),
      body: ref.watch(futureProvider).when(data: (data) => Column(
        children: [
          ElevatedButton(onPressed: () => ref.read(paramProvider.notifier).state = 'new value',
              child: const Text('pretend update')),
          Text(data)],
      ),
          error: (_,__) => const Text('something went wrong'),
          loading: () => const CircularProgressIndicator())
  );
}

final paramProvider = StateProvider((ref) => 'Initial value');

final futureProvider = FutureProvider.autoDispose<String>((ref) async {
  String parameter = ref.watch(paramProvider);
  return pretendApiCall(parameter);
});
Future<String> pretendApiCall(String parameter) async {
  await Future.delayed(const Duration(seconds: 3));
  return parameter;
}

Try to upgrade your Flutter 3.3.0 And using hooks_riverpod: ^1.0.4尝试升级您的Flutter 3.3.0并使用hooks_riverpod: ^1.0.4

在此处输入图像描述

I think you're seeing this breaking changed behavior:我认为您正在看到这种破坏性改变的行为:

Breaking After a provider has emitted an AsyncValue.data or AsyncValue.error, that provider will no longer emit an AsyncValue.loading.中断在提供程序发出 AsyncValue.data 或 AsyncValue.error 后,该提供程序将不再发出 AsyncValue.loading。 Instead, it will re-emit the latest value, but with the property AsyncValue.isRefreshing to true.相反,它将重新发出最新值,但属性 AsyncValue.isRefreshing 为 true。

So the behavior you describe is by design.因此,您描述的行为是设计使然。

Ah.啊。

var asyncValue = ref.watch(futureProvider);
return Scaffold(
  appBar: AppBar(title: const Text('Riverpod Spinner Question'),),
  body: asyncValue.when(data: (data) {
    if (asyncValue.isRefreshing) return const CircularProgressIndicator();
    return Column(
    children: [
      ElevatedButton(onPressed: () => ref.read(paramProvider.notifier).state = 'new value',
          child: const Text('pretend update')),
      Text(data)],
  );
  },

ah - the real answer from Remi himself https://github.com/rrousselGit/riverpod/discussions/1600 "This behavior has changed on the master branch啊 - 雷米本人的真正答案https://github.com/rrousselGit/riverpod/discussions/1600 “这种行为在主分支上发生了变化

Simply wait for a release and your problem should disappear"只需等待发布,您的问题就会消失”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM