locales method Null safety

Future<List<LocaleName>> locales()

returns the list of speech locales available on the device.

This method is useful to find the identifier to use for the listen method, it is the localeId member of the LocaleName.

Each LocaleName in the returned list has the identifier for the locale as well as a name for display. The name is localized for the system locale on the device.

Implementation

Future<List<LocaleName>> locales() async {
  if (!_initWorked) {
    throw SpeechToTextNotInitializedException();
  }
  final locales = await SpeechToTextPlatform.instance.locales();
  var filteredLocales = locales
      .map((locale) {
        var components = locale.split(':');
        if (components.length != 2) {
          return null;
        }
        return LocaleName(components[0], components[1]);
      })
      .where((item) => item != null)
      .toList()
      .cast<LocaleName>();
  if (filteredLocales.isNotEmpty) {
    _systemLocale = filteredLocales.first;
  } else {
    _systemLocale = null;
  }
  filteredLocales.sort((ln1, ln2) => ln1.name.compareTo(ln2.name));
  return filteredLocales;
}