Flutter Riverpod:成员“state”只能在“package:state_notifier/state_notifier.dart”子类的实例成员中使用

Zef*_*ndo 6 flutter riverpod

我有一个关于切换小部件的简单示例,我的期望是单击按钮后我显示圆形进度指示器,然后 3 秒后我显示文本。对于我的示例,我使用Riverpod_hooksflutter_hooks

加载提供者

class IsLoading extends StateNotifier<bool> {
  IsLoading() : super(false);
  void toggleLoading(bool value) => state = value;
}

final isLoadingProvider = StateNotifierProvider((ref) => IsLoading());
Run Code Online (Sandbox Code Playgroud)

主dart

class TestingApp extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final IsLoading isLoading = useProvider(isLoadingProvider);
    return Scaffold(
      body: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Center(
              child: isLoading.state ? CircularProgressIndicator() : Text('This Is Your Data'),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: FlatButton(
                  onPressed: () => showLoading(isLoading), child: Text('Toggle Loading')),
            ),
          ],
        ),
      ),
    );
  }

  void showLoading(IsLoading isLoading) async {
    isLoading.toggleLoading(true);
    print("Proses");
    await Future.delayed(const Duration(seconds: 3));
    print("Done");
    isLoading.toggleLoading(false);
  }
}

Run Code Online (Sandbox Code Playgroud)

但是当我按下按钮时,它不显示进度指示器,并且仍然显示文本,我收到此警告:
The member 'state' can only be used within instance members of subclasses of 'package:state_notifier/state_notifier.dart'.

我错过了什么吗?

我的源代码遵循这样的文档:

在此输入图像描述

小智 1

它就在那里说,当您像这样调用增量时,它不会\xe2\x80\x99t 触发重建方法。

\n

因此,不要使用 isLoading.toggleLoading(true)\n 调用 read 方法,这样它就可以像这样重建状态\nisLoading.read(context).toggleLoading(true)

\n