Flutter Hooks使用效果文档
signupHelper.state.success
我在我的 onSubmit 事件中调度了一个 API 请求,该请求具有变为 true的副作用。当成功 == true 时,我想导航到另一个屏幕。相反,我得到一个错误setState() or markNeedsBuild() called during build
我目前的解决方法是在导航前等待 50 毫秒,以确保没有进行重建。
我的代码看起来像这样
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import '../hooks/use_signup_helper.dart'
class SignupForm extends HookWidget {
const SignupForm({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
// this wraps useReducer and gives me access to state and dispatch
final signupHelper = useSignupHelper();
useEffect(() {
if (signupHelper.state.success) {
// this is my workaround - delay 50 milliseconds to avoid rebuild conflict
// Future<void>.delayed(const Duration(milliseconds: 50))
// .then((_) => Navigator.pushNamed(context, '/home'));
Navigator.pushNamed(context, '/home'));
}
return null;
}, [signupHelper.state.success]);
return ... // UI goes here