I have a problem concerning the latest updated bloc state. I have a button with onPressed function:
- First i trigger an Event, which changes the state
- Immediately after i retrieve the state, but its value is not updated to the latest event. To retrieve the state i use state from the BlocBuilder or the state from the context BlocProvider.of(context).state, but nothing works!!!
Do you have any ideas on how to retrieve the latest state version? Thanks. The code below:
class InputWidget extends StatefulWidget {
const InputWidget({Key? key}) : super(key: key);
@override
State<InputWidget> createState() => _InputWidgetState();
}
class _InputWidgetState extends State<InputWidget> {
late TextEditingController amountController;
String? categoryValue;
String? typeValue;
@override
void initState() {
super.initState();
amountController = TextEditingController();
}
@override
void dispose() {
amountController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return BlocBuilder<QuizBloc, QuizState>(
builder: (context, state) {
return Form(
child: Column(
children: [
ElevatedButton(
onPressed: () {
BlocProvider.of<QuizBloc>(context).add(
const QuizEvent.getQuizPressed(),
);
// Here i retrieve the state, the aim is to navigate to another page if
//state is...
if (BlocProvider.of<QuizBloc>(context).state.value || state.value) {
context.router.push(const QuizRoute());}
},
child: const Text(
'Start the quiz',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.white,
),
),
),
],
),
);
},
);
}
}