Description
For complete details, see flutter/flutter#86621
Description
When calling SpeechRecognitionResult.item
in a SpeechRecognition Dart sample on Dart 2.12 using dart html, doesn't throw any errors.
However, something changed since 2.13, and the same example throws SpeechRecognitionResult.item is not a function or method
.
Steps to reproduce just using dart example
Using the latest Dart version
dart pub global activate webdev
dart create -t web-simple quickstart
Paste in the code sample below in main.dart
minimal code dart sample
import 'dart:html' as html;
int _counter = 0;
String _recognized = '';
html.SpeechRecognition? _webSpeech;
void _incrementCounter() {
_webSpeech?.stop();
_webSpeech?.onResult.listen((speechEvent) => _onResult(speechEvent));
_webSpeech?.interimResults = true;
_webSpeech?.continuous = true;
_webSpeech?.start();
_counter++;
}
void _onResult(html.SpeechRecognitionEvent event) {
var results = event.results;
if (null == results) return;
for (html.SpeechRecognitionResult recognitionResult in results) {
if (null == recognitionResult.length || recognitionResult.length == 0) {
continue;
}
for (var altIndex = 0; altIndex < recognitionResult.length!; ++altIndex) {
html.SpeechRecognitionAlternative alt =
recognitionResult.item(altIndex);
if (null != alt.transcript && null != alt.confidence) {
_recognized = alt.transcript!;
}
}
}
}
void main() {
if (html.SpeechRecognition.supported) {
_webSpeech = html.SpeechRecognition();
}
_incrementCounter();
}
webdev serve
and open localhost address provided by the CLI into a browser on desktop- Allow microphone permission and say
Hello
or any word into the microphone and checkout browser console
This line in https://raw.githubusercontent.com/dart-lang/sdk/main/sdk/lib/html/dart2js/html_dart2js.dart throws the error
class SpeechRecognitionResult extends Interceptor {
// To suppress missing implicit constructor warnings.
factory SpeechRecognitionResult._() {
throw new UnsupportedError("Not supported");
}
bool? get isFinal native;
int? get length native;
SpeechRecognitionAlternative item(int index) native;
}
When checking js source, I noticed older Dart version used to have
let alt = recognitionResult.item(altIndex);
But in later Dart SDK this is
let alt = recognitionResult[$item](altIndex);
When using 2.12 Dart SDK, there is no issue
https://github.com/dart-lang/sdk/releases/tag/2.12.0-285.0.dev
For a Flutter example see flutter/flutter#86621 (comment)
This bug also affects a package csdcorp/speech_to_text#242