Implementing App Links in Flutter allows your application to open directly from a web URL, bypassing the browser and creating a seamless user experience. This feature is essential for deep linking, marketing campaigns, and improving retention by keeping users within your app ecosystem. To achieve this, you must configure both the native Android and iOS projects and ensure your domain hosts a valid asset verification file.
What You'll Need
- A Flutter project with a stable channel SDK (version 3.x or later recommended).
- An HTTPS domain that you control and can upload files to.
- Android Studio for configuring Android manifest files and intent filters.
- Xcode for configuring iOS Associated Domains entitlements.
- The
app_linkspackage added to yourpubspec.yaml.
Step-by-Step
- Add the Package: Open your terminal and run
flutter pub add app_links. This library handles the platform-specific logic for receiving link events in Dart. - Configure Android Intent Filters: Navigate to
android/app/src/main/AndroidManifest.xml. Inside the<activity>tag for your main activity, add an<intent-filter>block. Set the action toandroid.intent.action.VIEW, categories toDEFAULTandBROWSABLE, and include a<data>element specifying your scheme (https), host (yourdomain.com), and path prefix if needed. - Enable Auto Verify on Android: In the same intent filter, add the attribute
android:autoVerify="true". This tells Android to automatically verify the association between your app and the domain using the Digital Asset Links protocol. - Configure iOS Associated Domains: Open your iOS project in Xcode. Select your target, go to the Signing & Capabilities tab, and click the plus button to add the Associated Domains capability. Add an entry starting with
applinks:followed by your domain (e.g.,applinks:yourdomain.com). Do not include http:// or https:// in this string. - Upload Verification Files: For Android, create a file named
assetlinks.jsonat the root of your domain's.well-knowndirectory. It must contain your package name and SHA-256 fingerprint of your release keystore. For iOS, create anapple-app-site-associationfile in the same.well-knowndirectory, listing your Team ID and Bundle Identifier. - Listen for Links in Flutter: In your main Dart file, initialize the listener before running the app. Use
AppLinks().getInitialLink()to handle links when the app is cold-started, andAppLinks().linkStream.listen()to handle links when the app is already running in the background. - Handle Navigation: Parse the URI received from the stream. Extract the path segments and use them to navigate to the correct screen using your preferred router (such as GoRouter or Navigator). Ensure you check for null values to prevent crashes if the link structure is unexpected.
Common Mistakes
One frequent error is forgetting to enable HTTPS. App Links strictly require secure connections; HTTP URLs will never trigger the deep link behavior. Another common issue is mismatching the SHA-256 fingerprint on Android. Developers often use the debug key during testing but forget to update the assetlinks.json file with the release key before publishing, causing verification to fail in production.
On iOS, developers sometimes incorrectly format the Associated Domains entry. Including the protocol (like https://) or the trailing slash will break the configuration. Additionally, failing to restart the device after changing Apple Developer Portal settings can cause delays in propagating the new domain associations.
Tips
Use tools like Firebase Hosting or static site generators to easily deploy the required JSON files to your .well-known directory. These services usually handle the MIME types correctly, which is crucial because browsers and OS verifiers expect specific content types for these files.
When testing locally, use ngrok or similar tunneling services to expose your local development server to the internet. This allows you to test the full flow without deploying to production immediately. Remember to update your verification files to point to the temporary tunnel URL during testing, then revert them before release.
Troubleshooting
If links do not open the app, first verify that the JSON files are accessible via a direct browser request. If they return a 404 or HTML error page, the path or filename is incorrect. On Android, you can check verification status by opening Chrome and navigating to chrome://flags/#enable-link-to-app or using ADB commands to inspect the intent resolution.
For iOS issues, ensure that the apple-app-site-association file is served with the correct content type (application/json). Some CDNs default to text/plain, which causes silent failures. Clear Safari cache and restart the device after making changes to force a re-fetch of the association file.
Frequently Asked Questions
Do I need a separate package for Universal Links?
No, the app_links package handles both Android App Links and iOS Universal Links through a unified API, simplifying cross-platform implementation.
Can I use App Links without owning the domain?
You cannot fully implement standard App Links without control over the domain to host the verification files. However, you can use custom schemes (deep links) which do not require domain ownership, though they offer less security and reliability.
Why does my link open the browser instead of the app?
This usually indicates a failed verification. Check that your SHA-256 key matches the installed APK/IPA and that the JSON files are correctly formatted and accessible via HTTPS.
Does this work for private apps?
Yes, but only if the user has already installed the app. App Links rely on the OS recognizing the domain association with the installed package. They do not prompt installation if the app is missing.