[ad_1]
I’m constructing a easy calculator and utilizing Riverpod for state administration. Although I can replace state, the UI is just not being up to date with the adjustments… Can somebody inform me what I am doing mistaken ?? This is the code:
Calculator Mannequin
class CalculatorModel {
closing bool shouldAppend;
closing String equation;
closing String outcome;
const CalculatorModel(
{this.shouldAppend = true, this.equation = '0', this.outcome="0"});
CalculatorModel copyWith({
bool? shouldAppend,
String? equation,
String? outcome,
}) =>
CalculatorModel(
shouldAppend: shouldAppend ?? this.shouldAppend,
equation: equation ?? this.equation,
outcome: outcome ?? this.outcome);
}
Calculator State Notifier Implementation
import 'package deal:flutter_riverpod/flutter_riverpod.dart';
import 'package deal:parts/calculator_components/calculator_model.dart';
import 'package deal:math_expressions/math_expressions.dart';
closing calculatorProvider =
StateNotifierProvider<CalculatorStateNotifier, Checklist<CalculatorModel>>(
(ref) => CalculatorStateNotifier());
class CalculatorStateNotifier extends StateNotifier<Checklist<CalculatorModel>> {
CalculatorStateNotifier() : tremendous([const CalculatorModel()]);
void append(String calcInput) {
closing equation = () {
return state[0].equation == '0'
? calcInput
: state[0].equation + calcInput;
}();
state[0] = CalculatorModel(equation: equation);
}
}
Click on perform for calculator buttons. State is getting up to date, efficiently…
void onClickedButton(String calcInput, WidgetRef ref) {
ref.learn(calculatorProvider.notifier).append(calcInput);
ref.watch(calculatorProvider);
print(ref.watch(calculatorProvider)[0].equation);
}
Riverpod not updating UI when known as within the presentation layer…
@override
Widget construct(BuildContext context, WidgetRef ref) {
Dimension measurement = MediaQuery.of(context).measurement;
return Container(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
margin: const EdgeInsets.symmetric(vertical: 10),
width: measurement.width * 0.8,
youngster: Column(youngsters: [
Expanded(
child: Container(
child: Padding(
padding: const EdgeInsets.only(top: 15.0, right: 22),
child: Consumer(builder: (context, ref, _) {
return buildCalculatorScreen(
ref.watch(calculatorProvider)[0].equation,
ref.watch(calculatorProvider)[0].outcome);
}),
)),
),
]),
);
}
}
[ad_2]
