Unlocking Voice Commands: Integrating Google Assistant and Siri with Flutter Apps
The Flutter news you actually need
No spam, ever. Unsubscribe in one click.
Voice assistants like Google Assistant and Siri offer a hands-free, futuristic way for users to interact with your app. Imagine a user cooking and saying, “Hey Google, open RecipeWizard,” or driving and telling Siri to start their favorite meditation app. Integrating this functionality isn’t about writing Flutter code that listens for a wake word—it’s about teaching the operating system to associate your app with specific voice commands.
The core mechanism both platforms use is deep linking. When a user says “Open [App Name],” the OS needs to know which app to launch and what to do once it’s open. This requires configuring the native layers of your Flutter project.
Configuring Android for Google Assistant
On Android, the entry point is an Intent Filter. You define this in your AndroidManifest.xml file, located at android/app/src/main/.
You need to declare which “actions” and “categories” your app can handle. The standard action for being launched is android.intent.action.MAIN, and the category for being a launcher is android.intent.category.LAUNCHER. This is already present in your default Flutter project. To support being opened by voice, you ensure your main activity is configured to handle the VIEW action with the BROWSABLE category, which allows it to be triggered from external sources like Google Assistant.
Here’s a typical configuration for your main activity inside the <application> tag:
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Intent filter for deep linking/voice commands -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" />
</intent-filter>
</activity>
The key addition is the second <intent-filter>. It declares that your app can handle VIEW actions for URLs starting with myapp://. This is your custom deep link scheme. With this, the command “Hey Google, open MyApp” should work, as Google Assistant will attempt to launch an app that can handle a myapp:// URL.
Configuring iOS for Siri
iOS uses a similar concept called URL Schemes. You define these in your Info.plist file, located at ios/Runner/.
You need to add a CFBundleURLTypes entry. This can be done directly in Xcode by opening the ios/Runner.xcworkspace or by editing the Info.plist file as raw XML.
Here’s how the relevant section looks in the Info.plist file:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>
This tells iOS that your app registers for the myapp:// URL scheme. Now, when Siri processes the command “Hey Siri, open MyApp,” it will know to launch your app.
Handling the Deep Link in Flutter
Once the native layer launches your app via a deep link (like myapp://), you need to catch that link within your Flutter code and route the user appropriately. The uni_links package is excellent for this.
First, add the dependency to your pubspec.yaml:
dependencies:
uni_links: ^0.5.1
Then, in your app’s main entry point, you can set up a listener. A robust approach is to use it in your root widget’s initState.
import 'package:uni_links/uni_links.dart';
import 'package:flutter/services.dart' show PlatformException;
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Uri? _initialUri;
Uri? _latestUri;
@override
void initState() {
super.initState();
_initUniLinks();
}
Future<void> _initUniLinks() async {
// Handle initial link (app was cold-started by the link)
try {
final initialUri = await getInitialUri();
if (initialUri != null) {
setState(() => _initialUri = initialUri);
_navigateBasedOnLink(initialUri);
}
} on PlatformException catch (e) {
// Handle exception
}
// Listen for links while the app is running
uriLinkStream.listen((Uri? uri) {
if (uri != null) {
setState(() => _latestUri = uri);
_navigateBasedOnLink(uri);
}
});
}
void _navigateBasedOnLink(Uri uri) {
// Example: myapp://home or myapp://profile/123
if (uri.path.startsWith('/profile')) {
final id = uri.pathSegments.length > 1 ? uri.pathSegments[1] : null;
// Use your router (e.g., GoRouter, Navigator) to go to profile page
// Navigator.push(context, MaterialPageRoute(builder: (context) => ProfilePage(id: id)));
}
// Add more route handling logic
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Voice Command Demo')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Initial Deep Link: ${_initialUri?.toString() ?? "None"}'),
Text('Latest Deep Link: ${_latestUri?.toString() ?? "None"}'),
],
),
),
);
}
}
Common Pitfalls and Testing
- Caching: After making native configuration changes, do a full reinstall (
flutter runor a fresh install from the store). The OS caches app capabilities. - Scheme Uniqueness: Use a unique URL scheme (like
myappname123) to avoid conflicts with other apps. - Testing: On Android, you can test with
adb:
On iOS, you can test by typing your custom URL (adb shell am start -a android.intent.action.VIEW -d "myapp://home"myapp://home) directly into Safari’s address bar, which should prompt to open your app.
By configuring these native hooks and using uni_links to handle the incoming data, you enable users to launch your Flutter app with a simple voice command.
This blog is produced with the assistance of AI by a human editor. Learn more
Related Posts
Localizing Dynamic Content in Flutter: A Guide to Backend-Driven Translations
Many Flutter apps need to display content that changes based on user locale, but also comes from a backend (like Firebase). This post will explore best practices for fetching and integrating dynamic, localized content from a backend, ensuring a seamless user experience across different languages and regions without hardcoding translations.
Unraveling Type Mismatch Errors in Flutter: A Guide to 'X can't be assigned to Y' and '_InternalLinkedHashMap' Issues
Developers frequently encounter cryptic type mismatch errors like 'The argument type X can't be assigned to the parameter type Y' or '_InternalLinkedHashMap has no instance method 'cast''. This post will demystify these common Flutter/Dart type errors, explain their root causes (e.g., conflicting imports, dynamic typing pitfalls, JSON deserialization issues), and provide practical solutions to diagnose and fix them, improving code robustness and reducing debugging time.
Solving Flutter Web Memory Leaks: A Practical Guide to Identifying and Fixing Performance Issues
Flutter Web applications can suffer from increasing memory usage over time, leading to performance degradation. This post will delve into common causes of memory leaks in Flutter Web, provide practical debugging techniques using browser developer tools and Dart DevTools, and offer actionable strategies to identify and fix these issues for a smoother user experience.