'In my flutter App, First time it changes the state but second time it does not change the state and remain initState but performing action smoothing
I'm using the flutter speech_to_text library to listen and perform some action when I run the app everything remains perfect but when I go to the previous screen and then come back again the state is not getting changed and always running InitState method from my cubit class, but the function is running perfectly just state is not getting changed. Please help me. Here is my UI Code,
onTap: () {
checkMicState(context)
},
checkMicState(BuildContext context) {
setState(() {
mic = !mic;
listen(context);
});
}
listen(BuildContext context) async {
if (mic) {
bool available = await speech.initialize(
onStatus: (val) {
print(val);
switch (val) {
case 'listening':
BlocProvider.of<SearchCubit>(context).updateListening();
break;
case 'not listening':
BlocProvider.of<SearchCubit>(context).updateNotListening();
setState(() {
mic = false;
});
break;
case 'done':
BlocProvider.of<SearchCubit>(context).updateNotListening();
setState(() {
mic = false;
});
if (_text.length > 2) {
fetchResults(context, _text);
}
break;
default:
break;
}
},
onError: (val) => 'onError: $val',
);
if (available) {
setState(() => mic = true);
speech.listen(
onResult: (val) {
setState(() {
_text = val.recognizedWords;
});
if (_text.length > 0) {
BlocProvider.of<SearchCubit>(context)
.updateText(val.recognizedWords);
}
},
);
}
} else {
setState(() => mic = false);
speech.stop();
}
}
In this listen method main problem is creating, I have printed the value, value is correct "listening" but the state is not getting called "UpdateListening" instead of this InitState from my cubit is being called. But the First time it is running perfectly just the time the second time it is creating a mess.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
