我有一个关于切换小部件的简单示例,单击按钮后我的期望是显示circularProgressIndicator,然后三秒钟后显示文本。在我的示例中,我使用riverpod_hooks和flutter_hooks。
class IsLoading extends StateNotifier<bool> {
IsLoading() : super(false);
void toggleLoading(bool value) => state = value;
}
final isLoadingProvider = StateNotifierProvider((ref) => IsLoading());
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);
}
}
但是当我按下按钮时,它没有显示progressIndicator并且仍在显示文本,我得到了这个警告:
The member 'state' can only be used within instance members of subclasses of 'package:state_notifier/state_notifier.dart'.
我错过了什么吗?
我的源代码遵循如下文档:
答案 0 :(得分:0)
它说在那儿,这样调用增量时不会触发重建方法。
so而不是isLoading.toggleLoading(true) 用read方法调用,以便可以像这样重建状态 isLoading.read(context).toggleLoading(true)