Flutter Hooks useEffect Docs
I dispatch an API request in my onSubmit event that has a side effect of turning signupHelper.state.success
to true. I would like to navigate to another screen when success == true. Instead I get an error for setState() or markNeedsBuild() called during build
My current workaround is to wait 50 milliseconds before navigation to make sure there is no rebuild going on.
My code looks like this
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
It looks like we can use the ScheduleBinding and SchedulerPhase class. It is imported like this -
And the new useEffect function looks like this -