Skip to content
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

Closed
JulianPscheid opened this issue May 14, 2024 · 3 comments
Closed

Endless Streaming Bad state: No element errors #59

JulianPscheid opened this issue May 14, 2024 · 3 comments

Comments

@JulianPscheid
Copy link

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.

@JulianPscheid
Copy link
Author

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.. (package:mobile/controllers/audio_recognize_controller.dart:130:52)
E/flutter (23333): #2 MappedListIterable.elementAt (dart:_internal/iterable.dart:425:31)
E/flutter (23333): #3 ListIterable.join (dart:_internal/iterable.dart:159:25)
E/flutter (23333): #4 AudioRecognizeController.streamingRecognize. (package:mobile/controllers/audio_recognize_controller.dart:130:70)
E/flutter (23333): #5 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter (23333): #6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter (23333): #7 _DelayedData.perform (dart:async/stream_impl.dart:515:14)
E/flutter (23333): #8 _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
E/flutter (23333): #9 _PendingEvents.schedule. (dart:async/stream_impl.dart:591:7)
E/flutter (23333): #10 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter (23333): #11 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

@ibrahimdevs
Copy link

@felixjunghans I'm experiencing the similar problem. I think the suggested fix seems ok, could you take a look?

@felixjunghans
Copy link
Owner

Hey @JulianPscheid and @ibrahimdevs thanks for the suggestion. I have updated the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants