diff --git a/speech_to_text/lib/speech_to_text.dart b/speech_to_text/lib/speech_to_text.dart index 195c8e9..2a2a6aa 100644 --- a/speech_to_text/lib/speech_to_text.dart +++ b/speech_to_text/lib/speech_to_text.dart @@ -19,6 +19,16 @@ class LocaleName { final String name; LocaleName(this.localeId, this.name); + + @override + bool operator ==(dynamic other) { + if (other.runtimeType != runtimeType) return false; + final LocaleName typedOther = other; + return this.localeId == typedOther.localeId; + } + + @override + int get hashCode => localeId.hashCode; } /// Notified as words are recognized with the current set of recognized words. @@ -578,6 +588,7 @@ class SpeechToText { return LocaleName(components[0], components[1]); }) .where((item) => item != null) + .toSet() .toList() .cast(); if (filteredLocales.isNotEmpty) { diff --git a/speech_to_text/test/speech_to_text_test.dart b/speech_to_text/test/speech_to_text_test.dart index 450aa67..5cea330 100644 --- a/speech_to_text/test/speech_to_text_test.dart +++ b/speech_to_text/test/speech_to_text_test.dart @@ -558,6 +558,20 @@ void main() { expect(localeNames[1].localeId, TestSpeechChannelHandler.localeId2); expect(localeNames[1].name, TestSpeechChannelHandler.name2); }); + test('deduplicates returned locales', () async { + await speech.initialize(); + testPlatform.localesResult.addAll([ + TestSpeechChannelHandler.locale1, + TestSpeechChannelHandler.locale2, + TestSpeechChannelHandler.locale1 + ]); + var localeNames = await speech.locales(); + expect(localeNames, hasLength(2)); + expect(localeNames[0].localeId, TestSpeechChannelHandler.localeId1); + expect(localeNames[0].name, TestSpeechChannelHandler.name1); + expect(localeNames[1].localeId, TestSpeechChannelHandler.localeId2); + expect(localeNames[1].name, TestSpeechChannelHandler.name2); + }); test('skips incorrect locales', () async { await speech.initialize(); testPlatform.localesResult.addAll([