I'd like to reuse one function to update different state variables of a StatefulWidget
. Which state variable gets updated should depend on the variable reference which is passed to the function as a parameter. Here's some example code:
class _MyScreenState extends State<MyScreen> {
String stateVariable = 'initial value';
void update(String variableRef) {
setState((() {
variableRef = 'updated value';
})
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => update(stateVariable),
child: Text(stateVariable)
)
}
}
The state is set successfully in the setState
function, but unfortunately the state change is not represented in the UI, i.e. the build
function is not called again.
My expectation here would be that the stateVariable
should be updated since I pass the variable to the update
function by reference. Is this assumption false?
Do you know a way of passing a state variable by reference so that it can be updated idempotently by a function?