From 0a791b0f15a11541a069c24075d6f0babb69e5dd Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 22 Mar 2024 00:36:17 -0700 Subject: [PATCH] applying static analysis and fixing issues --- analysis_options.yaml | 34 ++++++---------------------------- example/lib/main.dart | 39 ++++++++++++++++++++------------------- example/pubspec.yaml | 2 ++ lib/flutter_tts.dart | 6 +++--- lib/flutter_tts_web.dart | 30 +++++++++++++++--------------- pubspec.yaml | 2 ++ 6 files changed, 48 insertions(+), 65 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index ac75a3a1..c484bbdd 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,33 +1,11 @@ +include: package:lints/recommended.yaml + analyzer: + language: + strict-casts: true + strict-inference: true + strict-raw-types: true errors: unused_import: error unused_local_variable: error dead_code: error -linter: - rules: - - avoid_empty_else - - comment_references - - control_flow_in_finally - - empty_statements - - hash_and_equals - - only_throw_errors - - test_types_in_equals - - throw_in_finally - - unrelated_type_equality_checks - - valid_regexps - - avoid_init_to_null - - avoid_return_types_on_setters - - await_only_futures - - camel_case_types - - directives_ordering - - empty_catches - - empty_constructor_bodies - - library_names - - library_prefixes - - non_constant_identifier_names - - omit_local_variable_types - - prefer_final_fields - - prefer_is_not_empty - - prefer_typing_uninitialized_variables - - slash_for_doc_comments - - type_init_formals \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index 03ec519e..7de70d54 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -28,10 +28,10 @@ class _MyAppState extends State { TtsState ttsState = TtsState.stopped; - get isPlaying => ttsState == TtsState.playing; - get isStopped => ttsState == TtsState.stopped; - get isPaused => ttsState == TtsState.paused; - get isContinued => ttsState == TtsState.continued; + bool get isPlaying => ttsState == TtsState.playing; + bool get isStopped => ttsState == TtsState.stopped; + bool get isPaused => ttsState == TtsState.paused; + bool get isContinued => ttsState == TtsState.continued; bool get isIOS => !kIsWeb && Platform.isIOS; bool get isAndroid => !kIsWeb && Platform.isAndroid; @@ -44,7 +44,7 @@ class _MyAppState extends State { initTts(); } - initTts() { + dynamic initTts() { flutterTts = FlutterTts(); _setAwaitOptions(); @@ -101,21 +101,21 @@ class _MyAppState extends State { Future _getEngines() async => await flutterTts.getEngines; - Future _getDefaultEngine() async { + Future _getDefaultEngine() async { var engine = await flutterTts.getDefaultEngine; if (engine != null) { print(engine); } } - Future _getDefaultVoice() async { + Future _getDefaultVoice() async { var voice = await flutterTts.getDefaultVoice; if (voice != null) { print(voice); } } - Future _speak() async { + Future _speak() async { await flutterTts.setVolume(volume); await flutterTts.setSpeechRate(rate); await flutterTts.setPitch(pitch); @@ -127,16 +127,16 @@ class _MyAppState extends State { } } - Future _setAwaitOptions() async { + Future _setAwaitOptions() async { await flutterTts.awaitSpeakCompletion(true); } - Future _stop() async { + Future _stop() async { var result = await flutterTts.stop(); if (result == 1) setState(() => ttsState = TtsState.stopped); } - Future _pause() async { + Future _pause() async { var result = await flutterTts.pause(); if (result == 1) setState(() => ttsState = TtsState.paused); } @@ -147,11 +147,12 @@ class _MyAppState extends State { flutterTts.stop(); } - List> getEnginesDropDownMenuItems(dynamic engines) { + List> getEnginesDropDownMenuItems( + List engines) { var items = >[]; for (dynamic type in engines) { items.add(DropdownMenuItem( - value: type as String?, child: Text(type as String))); + value: type as String?, child: Text((type as String)))); } return items; } @@ -165,11 +166,11 @@ class _MyAppState extends State { } List> getLanguageDropDownMenuItems( - dynamic languages) { + List languages) { var items = >[]; for (dynamic type in languages) { items.add(DropdownMenuItem( - value: type as String?, child: Text(type as String))); + value: type as String?, child: Text((type as String)))); } return items; } @@ -222,7 +223,7 @@ class _MyAppState extends State { future: _getEngines(), builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasData) { - return _enginesDropDownSection(snapshot.data); + return _enginesDropDownSection(snapshot.data as List); } else if (snapshot.hasError) { return Text('Error loading engines...'); } else @@ -236,7 +237,7 @@ class _MyAppState extends State { future: _getLanguages(), builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasData) { - return _languageDropDownSection(snapshot.data); + return _languageDropDownSection(snapshot.data as List); } else if (snapshot.hasError) { return Text('Error loading languages...'); } else @@ -271,7 +272,7 @@ class _MyAppState extends State { ); } - Widget _enginesDropDownSection(dynamic engines) => Container( + Widget _enginesDropDownSection(List engines) => Container( padding: EdgeInsets.only(top: 50.0), child: DropdownButton( value: engine, @@ -280,7 +281,7 @@ class _MyAppState extends State { ), ); - Widget _languageDropDownSection(dynamic languages) => Container( + Widget _languageDropDownSection(List languages) => Container( padding: EdgeInsets.only(top: 10.0), child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [ DropdownButton( diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 317cb199..1f16ca43 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -16,6 +16,8 @@ dev_dependencies: flutter_tts: path: ../ +dependency_overrides: + material_color_utilities: 0.11.1 # For information on the generic Dart part of this file, see the # following page: https://www.dartlang.org/tools/pub/pubspec diff --git a/lib/flutter_tts.dart b/lib/flutter_tts.dart index dbbeba78..5a350c02 100644 --- a/lib/flutter_tts.dart +++ b/lib/flutter_tts.dart @@ -3,7 +3,7 @@ import 'dart:io' show Platform; import 'package:flutter/services.dart'; -typedef void ErrorHandler(dynamic message); +typedef ErrorHandler = void Function(dynamic message); typedef ProgressHandler = void Function( String text, int start, int end, String word); @@ -326,7 +326,7 @@ class SpeechRateValidRange { // Provides Platform specific TTS services (Android: TextToSpeech, IOS: AVSpeechSynthesizer) class FlutterTts { - static const MethodChannel _channel = const MethodChannel('flutter_tts'); + static const MethodChannel _channel = MethodChannel('flutter_tts'); VoidCallback? startHandler; VoidCallback? completionHandler; @@ -585,7 +585,7 @@ class FlutterTts { } /// Platform listeners - Future platformCallHandler(MethodCall call) async { + Future platformCallHandler(MethodCall call) async { switch (call.method) { case "speak.onStart": if (startHandler != null) { diff --git a/lib/flutter_tts_web.dart b/lib/flutter_tts_web.dart index ca9ec69c..0632934a 100644 --- a/lib/flutter_tts_web.dart +++ b/lib/flutter_tts_web.dart @@ -8,25 +8,25 @@ import 'package:flutter_web_plugins/flutter_web_plugins.dart'; enum TtsState { playing, stopped, paused, continued } class FlutterTtsPlugin { - static const String PLATFORM_CHANNEL = "flutter_tts"; + static const String platformChannel = "flutter_tts"; static late MethodChannel channel; bool awaitSpeakCompletion = false; TtsState ttsState = TtsState.stopped; - Completer? _speechCompleter; + Completer? _speechCompleter; - get isPlaying => ttsState == TtsState.playing; + bool get isPlaying => ttsState == TtsState.playing; - get isStopped => ttsState == TtsState.stopped; + bool get isStopped => ttsState == TtsState.stopped; - get isPaused => ttsState == TtsState.paused; + bool get isPaused => ttsState == TtsState.paused; - get isContinued => ttsState == TtsState.continued; + bool get isContinued => ttsState == TtsState.continued; static void registerWith(Registrar registrar) { channel = - MethodChannel(PLATFORM_CHANNEL, const StandardMethodCodec(), registrar); + MethodChannel(platformChannel, const StandardMethodCodec(), registrar); final instance = FlutterTtsPlugin(); channel.setMethodCallHandler(instance.handleMethodCall); } @@ -40,9 +40,9 @@ class FlutterTtsPlugin { FlutterTtsPlugin() { try { - utterance = new js.JsObject( + utterance = js.JsObject( js.context["SpeechSynthesisUtterance"] as js.JsFunction, [""]); - synth = new js.JsObject.fromBrowserObject( + synth = js.JsObject.fromBrowserObject( js.context["speechSynthesis"] as js.JsObject); _listeners(); supported = true; @@ -91,9 +91,9 @@ class FlutterTtsPlugin { channel.invokeMethod("speak.onContinue", null); }; - utterance["onerror"] = (e) { + utterance["onerror"] = (Object e) { ttsState = TtsState.stopped; - var event = new js.JsObject.fromBrowserObject(e); + var event = js.JsObject.fromBrowserObject(e); if (_speechCompleter != null) { _speechCompleter = null; } @@ -189,7 +189,7 @@ class FlutterTtsPlugin { void _setLanguage(String? language) => utterance['lang'] = language; void _setVoice(Map voice) { var tmpVoices = synth.callMethod("getVoices"); - var targetList = tmpVoices.where((e) { + var targetList = tmpVoices.where((dynamic e) { return voice["name"] == e["name"] && voice["locale"] == e["lang"]; }); if (targetList.isNotEmpty as bool) { @@ -219,8 +219,8 @@ class FlutterTtsPlugin { voices = synth.callMethod("getVoices") as List; } - getVoices() async { - var tmpVoices = synth.callMethod("getVoices"); + Future>> getVoices() async { + var tmpVoices = synth.callMethod("getVoices") as List; var voiceList = >[]; for (var voice in tmpVoices) { voiceList.add({ @@ -232,7 +232,7 @@ class FlutterTtsPlugin { } void _setLanguages() { - var langs = Set(); + var langs = {}; for (var v in voices!) { langs.add(v['lang'] as String?); } diff --git a/pubspec.yaml b/pubspec.yaml index f0caeab0..1c3f6a82 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,3 +31,5 @@ flutter: environment: sdk: ">=2.15.0 <4.0.0" flutter: ">=1.22.0" +dev_dependencies: + lints: ^3.0.0