Skip to content

Commit

Permalink
feat: fix SpeechToTextPlatform.instance.locales() returns duplicates #…
Browse files Browse the repository at this point in the history
  • Loading branch information
sowens-csd committed Aug 14, 2024
1 parent 19293bb commit 85088b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions speech_to_text/lib/speech_to_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -578,6 +588,7 @@ class SpeechToText {
return LocaleName(components[0], components[1]);
})
.where((item) => item != null)
.toSet()
.toList()
.cast<LocaleName>();
if (filteredLocales.isNotEmpty) {
Expand Down
14 changes: 14 additions & 0 deletions speech_to_text/test/speech_to_text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit 85088b7

Please sign in to comment.