Skip to content

Commit

Permalink
applying static analysis and fixing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dlutton committed Mar 22, 2024
1 parent 3cb449d commit 0a791b0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 65 deletions.
34 changes: 6 additions & 28 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -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
39 changes: 20 additions & 19 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class _MyAppState extends State<MyApp> {

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;
Expand All @@ -44,7 +44,7 @@ class _MyAppState extends State<MyApp> {
initTts();
}

initTts() {
dynamic initTts() {
flutterTts = FlutterTts();

_setAwaitOptions();
Expand Down Expand Up @@ -101,21 +101,21 @@ class _MyAppState extends State<MyApp> {

Future<dynamic> _getEngines() async => await flutterTts.getEngines;

Future _getDefaultEngine() async {
Future<void> _getDefaultEngine() async {
var engine = await flutterTts.getDefaultEngine;
if (engine != null) {
print(engine);
}
}

Future _getDefaultVoice() async {
Future<void> _getDefaultVoice() async {
var voice = await flutterTts.getDefaultVoice;
if (voice != null) {
print(voice);
}
}

Future _speak() async {
Future<void> _speak() async {
await flutterTts.setVolume(volume);
await flutterTts.setSpeechRate(rate);
await flutterTts.setPitch(pitch);
Expand All @@ -127,16 +127,16 @@ class _MyAppState extends State<MyApp> {
}
}

Future _setAwaitOptions() async {
Future<void> _setAwaitOptions() async {
await flutterTts.awaitSpeakCompletion(true);
}

Future _stop() async {
Future<void> _stop() async {
var result = await flutterTts.stop();
if (result == 1) setState(() => ttsState = TtsState.stopped);
}

Future _pause() async {
Future<void> _pause() async {
var result = await flutterTts.pause();
if (result == 1) setState(() => ttsState = TtsState.paused);
}
Expand All @@ -147,11 +147,12 @@ class _MyAppState extends State<MyApp> {
flutterTts.stop();
}

List<DropdownMenuItem<String>> getEnginesDropDownMenuItems(dynamic engines) {
List<DropdownMenuItem<String>> getEnginesDropDownMenuItems(
List<dynamic> engines) {
var items = <DropdownMenuItem<String>>[];
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;
}
Expand All @@ -165,11 +166,11 @@ class _MyAppState extends State<MyApp> {
}

List<DropdownMenuItem<String>> getLanguageDropDownMenuItems(
dynamic languages) {
List<dynamic> languages) {
var items = <DropdownMenuItem<String>>[];
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;
}
Expand Down Expand Up @@ -222,7 +223,7 @@ class _MyAppState extends State<MyApp> {
future: _getEngines(),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.hasData) {
return _enginesDropDownSection(snapshot.data);
return _enginesDropDownSection(snapshot.data as List<dynamic>);
} else if (snapshot.hasError) {
return Text('Error loading engines...');
} else
Expand All @@ -236,7 +237,7 @@ class _MyAppState extends State<MyApp> {
future: _getLanguages(),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.hasData) {
return _languageDropDownSection(snapshot.data);
return _languageDropDownSection(snapshot.data as List<dynamic>);
} else if (snapshot.hasError) {
return Text('Error loading languages...');
} else
Expand Down Expand Up @@ -271,7 +272,7 @@ class _MyAppState extends State<MyApp> {
);
}

Widget _enginesDropDownSection(dynamic engines) => Container(
Widget _enginesDropDownSection(List<dynamic> engines) => Container(
padding: EdgeInsets.only(top: 50.0),
child: DropdownButton(
value: engine,
Expand All @@ -280,7 +281,7 @@ class _MyAppState extends State<MyApp> {
),
);

Widget _languageDropDownSection(dynamic languages) => Container(
Widget _languageDropDownSection(List<dynamic> languages) => Container(
padding: EdgeInsets.only(top: 10.0),
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
DropdownButton(
Expand Down
2 changes: 2 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lib/flutter_tts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -585,7 +585,7 @@ class FlutterTts {
}

/// Platform listeners
Future platformCallHandler(MethodCall call) async {
Future<dynamic> platformCallHandler(MethodCall call) async {
switch (call.method) {
case "speak.onStart":
if (startHandler != null) {
Expand Down
30 changes: 15 additions & 15 deletions lib/flutter_tts_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<dynamic>? _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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -189,7 +189,7 @@ class FlutterTtsPlugin {
void _setLanguage(String? language) => utterance['lang'] = language;
void _setVoice(Map<String?, String?> 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) {
Expand Down Expand Up @@ -219,8 +219,8 @@ class FlutterTtsPlugin {
voices = synth.callMethod("getVoices") as List<dynamic>;
}

getVoices() async {
var tmpVoices = synth.callMethod("getVoices");
Future<List<Map<String, String>>> getVoices() async {
var tmpVoices = synth.callMethod("getVoices") as List<dynamic>;
var voiceList = <Map<String, String>>[];
for (var voice in tmpVoices) {
voiceList.add({
Expand All @@ -232,7 +232,7 @@ class FlutterTtsPlugin {
}

void _setLanguages() {
var langs = Set<String?>();
var langs = <String?>{};
for (var v in voices!) {
langs.add(v['lang'] as String?);
}
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ flutter:
environment:
sdk: ">=2.15.0 <4.0.0"
flutter: ">=1.22.0"
dev_dependencies:
lints: ^3.0.0

0 comments on commit 0a791b0

Please sign in to comment.