I am trying out Bloc in flutter. It seems to work fine when calling a block function in a stateful widget for example:
RaisedButton(
onPressed: () => _nextStep(),
child: Text("Next"))
This would call the _nextStep() function and update the UI. The nextStep function:
_nextStep() async {
_bloc.StepEventSink.add(NextStep());
}
and I scafold the widget with StreamBuider and that works. But if i call _nextStep() outside of the class, the data updates but the UI don't. Example:
class FormWizard extends StatefulWidget {
@override
_FormWizardState createState() => _FormWizardState();
next() {
_FormWizardState()._nextStep();
}
}
How can I update the UI outside of the widget?
I solved my issue by using Provider State management. I declared a SteppBloc class and and assign the current step to 0. Then use notifyListeners to update the value on widgets that uses it.
StepBloc class:
Then I wrap the main Widget with ChangeNotifierProvider of StepperBloc