Skip to content

On Windows, stop() causes crash if no audio has been played yet #624

@josh2112

Description

@josh2112

🐛 Bug Report

On Windows, calling stop() causes the whole app to crash with no error or log messages unless speak() has first been called.

Expected behavior

No crash.

Reproduction steps

Run this extremely minimal app:

import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';

void main() => runApp(TtsTestApp());

class TtsTestApp extends StatefulWidget {
  const TtsTestApp({super.key});

  @override
  State<TtsTestApp> createState() => _TtsTestAppState();
}

class _TtsTestAppState extends State<TtsTestApp> {
  final tts = FlutterTts();
  bool isInitialized = false;

  @override
  void initState() {
    super.initState();

    tts.awaitSpeakCompletion(true).then((_) => setState(() => isInitialized = true));
  }

  @override
  Widget build(BuildContext context) => MaterialApp(
    home: Center(
      child: !isInitialized
          ? Text("Initializing...")
          : Row(
              children: [
                FilledButton(onPressed: () => tts.speak('Hello, this is a test of flutter_tts'), child: const Text('Say something')),
                FilledButton(onPressed: () => tts.stop(), child: const Text('Stop speech')),
              ],
            ),
    ),
  );
}

If you hit "Say something", then "Stop speech", everything works correctly: The sentence begins speaking and you can stop it mid-sentence.

If you then hit "Stop speech" again, there's no error (even though speech is not happening), as expected.

However, if you restart the app (full restart, not hot restart!) and hit "Stop speech" without first speaking anything, the whole application then crashes with no feedback besides "Lost connection to device".

I can work around this by keeping a flag that indicates whether speak() has been called or not, and if not, do not call stop(), but it probably should be fixed.

Configuration

Version: 4.2.3

Platform:

  • 📱 iOS
  • 🤖 Android
  • Windows

Output of flutter doctor -v:

[√] Flutter (Channel stable, 3.38.9, on Microsoft Windows [Version 10.0.22631.6199], locale en-US) [598ms]
    • Flutter version 3.38.9 on channel stable at D:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 67323de285 (3 weeks ago), 2026-01-28 13:43:12 -0800
    • Engine revision 587c18f873
    • Dart version 3.10.8
    • DevTools version 2.51.1
    • Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations, enable-native-assets,
      omit-legacy-version-file, enable-lldb-debugging

[√] Windows Version (11 Pro 64-bit, 23H2, 2009) [5.1s]

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [4.7s]
    • Android SDK at C:\Users\jf334.CEM\AppData\Local\Android\sdk
    • Emulator version 35.1.20.0 (build_id 12234392) (CL:N/A)
    • Platform android-36, build-tools 35.0.0
    • Java binary at: D:\Program Files\Android\Android Studio\jbr\bin\java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 17.0.11+0--11852314)
    • All Android licenses accepted.

[√] Chrome - develop for the web [123ms]
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Professional 2022 17.14.24) [121ms]
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Professional
    • Visual Studio Professional 2022 version 17.14.36908.2
    • Windows 10 SDK version 10.0.26100.0

[√] Connected device (3 available) [400ms]
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22631.6199]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 143.0.7499.193
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 144.0.3719.115

[√] Network resources [415ms]
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions