initialize method

Future<bool> initialize({
  1. SpeechErrorListener? onError,
  2. SpeechStatusListener? onStatus,
  3. dynamic debugLogging = false,
  4. Duration finalTimeout = defaultFinalTimeout,
  5. List<SpeechConfigOption>? options,
})

Initialize speech recognition services, returns true if successful, false if failed.

This method must be called before any other speech functions.

If this method returns false no further SpeechToText methods should be used. Should only be called once if successful but does protect itself if called repeatedly. False usually means that the user has denied permission to use speech. The usual option in that case is to give them instructions on how to open system settings and grant permission.

onError is an optional listener for errors like timeout, or failure of the device speech recognition.

onStatus is an optional listener for status changes. There are three possible status values:

  • listening when speech recognition begins after calling the listen method.
  • notListening when speech recognition is no longer listening to the microphone after a timeout, cancel or stop call.
  • done when all results have been delivered.

debugLogging controls whether there is detailed logging from the underlying platform code. It is off by default, usually only useful for troubleshooting issues with a particular OS version or device, fairly verbose

finalTimeout a duration to wait for a final result from the device speech recognition service. If no final result is received within this time the last partial result is returned as final. This defaults to two seconds. A duration of fifty milliseconds or less disables the check and final results will only be returned from the device.

options pass platform specific configuration options to the platform specific implementation.

Implementation

Future<bool> initialize(
    {SpeechErrorListener? onError,
    SpeechStatusListener? onStatus,
    debugLogging = false,
    Duration finalTimeout = defaultFinalTimeout,
    List<SpeechConfigOption>? options}) async {
  if (_initWorked) {
    return Future.value(_initWorked);
  }
  _finalTimeout = finalTimeout;
  if (finalTimeout <= _minFinalTimeout) {}
  errorListener = onError;
  statusListener = onStatus;
  SpeechToTextPlatform.instance.onTextRecognition = _onTextRecognition;
  SpeechToTextPlatform.instance.onError = _onNotifyError;
  SpeechToTextPlatform.instance.onStatus = _onNotifyStatus;
  SpeechToTextPlatform.instance.onSoundLevel = _onSoundLevelChange;
  _initWorked = await SpeechToTextPlatform.instance
      .initialize(debugLogging: debugLogging, options: options);
  return _initWorked;
}