我有一个关于切换小部件的简单示例,我的期望是单击按钮后我显示圆形进度指示器,然后 3 秒后我显示文本。对于我的示例,我使用Riverpod_hooks和flutter_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)
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| 归档时间: |
|
| 查看次数: |
4529 次 |
| 最近记录: |