Closed JulianPscheid closed 3 months ago
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
E/flutter (23333): #0 ListBase.first (dart:collection/list.dart:64:22)
E/flutter (23333): #1 AudioRecognizeController.streamingRecognize.
@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.