I just start to learn flutter, and I'm curious about how can i setState() dynamically in flutter
in React Native i usually used a function like this to setState dynamically:
class foo extends React.Component{
state={
username:null,
password:null
}
toggleSetState(whatState, value){
this.setState({ [whatState]: value })
}
render() {
return (
<View>
<TextInput
value={this.state.username}
onChangeText={(text)=>{toggleSetState(username, text)}
/>
<TextInput
value={this.state.password}
onChangeText={(text)=>{toggleSetState(password, text)}
/>
</View>
);
}
}
what is an equivalent of above code in Flutter?
I've tried this in Flutter but it seems doesn't work
class _LoginFormState extends State<LoginForm> {
String username, password;
void ToogleState(typedata, text){
setState(() {
typedata = text;
});
}
//Widget
TextField(
onChanged: (text){ToogleState(username, text); print(username);},
decoration: InputDecoration(
hintText: 'input username', labelText: 'Username'
),
),
}
setStateis meant for state changes which changes the ui like fetching data then rendering it into widgets. In your case, TextField is already handing majority of the visual changes so you should not need to callsetState. Flutter is not Reactjs.initStateand futures dartlang.org/tutorials/language/futures docs.flutter.io/flutter/widgets/State/initState.html Nothing is that complicated