-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Endless Streaming Bad state: No element errors #59
Comments
Here's the console output. Note that AudioRecognizeController builds on the example code from endless_streaming_example. E/flutter (23333): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: No element |
@felixjunghans I'm experiencing the similar problem. I think the suggested fix seems ok, could you take a look? |
Hey @JulianPscheid and @ibrahimdevs thanks for the suggestion. I have updated the example. |
Description
An unhandled exception is occasionally thrown in _AudioRecognizeState when trying to access the first element of an empty list. I only come across this error occasionally when testing the V2 endless stream. The error occurs in the streamingRecognize method, specifically in this line:
final currentText = data.results.map((e) => e.alternatives.first.transcript).join('\n');
The error message is "Bad state: No element", which is thrown when trying to access the first element of an empty list.
Steps to Reproduce
Unfortunately this error appears inconsistently. Sometimes I need to run streamingRecognize for up to 15 minutes before it occurs. Other times, it happens sooner.
Expected Behavior
The method should handle the case where alternatives is an empty list and not throw an exception.
Environment
Flutter SDK version: 3.3.4
Dart SDK version: 3.19.6
Operating System: Both iOS and Android
Potential Fix
Modify the line where the error occurs to check if alternatives is not empty before trying to access its first element. Here's the modified code:
final currentText = data.results .where((e) => e.alternatives.isNotEmpty) .map((e) => e.alternatives.first.transcript) .join('\n');
In this code, the where method is used to filter out the elements of data.results where alternatives is empty. This ensures that when we call alternatives.first.transcript, alternatives always has at least one element, so the "Bad state: No element" error won't be thrown.
The text was updated successfully, but these errors were encountered: