Skip to content

Commit

Permalink
feat: example updated with try
Browse files Browse the repository at this point in the history
  • Loading branch information
sowens-csd committed Dec 21, 2021
1 parent 70fa7d8 commit 0e507a1
Showing 1 changed file with 57 additions and 48 deletions.
105 changes: 57 additions & 48 deletions speech_to_text/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,31 @@ class _SpeechSampleAppState extends State<SpeechSampleApp> {
/// it can only be called once.
Future<void> initSpeechState() async {
_logEvent('Initialize');
var hasSpeech = await speech.initialize(
onError: errorListener,
onStatus: statusListener,
debugLogging: true,
);
if (hasSpeech) {
// Get the list of languages installed on the supporting platform so they
// can be displayed in the UI for selection by the user.
_localeNames = await speech.locales();

var systemLocale = await speech.systemLocale();
_currentLocaleId = systemLocale?.localeId ?? '';
try {
var hasSpeech = await speech.initialize(
onError: errorListener,
onStatus: statusListener,
debugLogging: true,
);
if (hasSpeech) {
// Get the list of languages installed on the supporting platform so they
// can be displayed in the UI for selection by the user.
_localeNames = await speech.locales();

var systemLocale = await speech.systemLocale();
_currentLocaleId = systemLocale?.localeId ?? '';
}
if (!mounted) return;

setState(() {
_hasSpeech = hasSpeech;
});
} catch (e) {
setState(() {
lastError = 'Speech recognition failed: ${e.toString()}';
_hasSpeech = false;
});
}

if (!mounted) return;

setState(() {
_hasSpeech = hasSpeech;
});
}

@override
Expand Down Expand Up @@ -348,36 +354,39 @@ class SessionOptionsWidget extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Row(
children: [
Text('Language: '),
DropdownButton<String>(
onChanged: (selectedVal) => switchLang(selectedVal),
value: currentLocaleId,
items: localeNames
.map(
(localeName) => DropdownMenuItem(
value: localeName.localeId,
child: Text(localeName.name),
),
)
.toList(),
),
],
),
Row(
children: [
Text('Log events: '),
Checkbox(
value: logEvents,
onChanged: switchLogging,
),
],
)
],
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: [
Text('Language: '),
DropdownButton<String>(
onChanged: (selectedVal) => switchLang(selectedVal),
value: currentLocaleId,
items: localeNames
.map(
(localeName) => DropdownMenuItem(
value: localeName.localeId,
child: Text(localeName.name),
),
)
.toList(),
),
],
),
Row(
children: [
Text('Log events: '),
Checkbox(
value: logEvents,
onChanged: switchLogging,
),
],
)
],
),
);
}
}
Expand Down

0 comments on commit 0e507a1

Please sign in to comment.