Flutter是否支持Firebase Dynamic Links?若支持,如何实现应用内内容跳转?
Absolutely! Flutter fully supports Firebase Dynamic Links, and setting it up to deep link users directly to your app's specific content (whether on iOS or Android) is totally doable. Let me break down the process step by step for you:
First off, make sure you have these ready:
- A Firebase project linked to your Flutter app (you'll need the
google-services.jsonfor Android andGoogleService-Info.plistfor iOS added to your project) - The latest stable version of Flutter installed
Open your pubspec.yaml file and add these dependencies under the dependencies section:
firebase_core: ^2.15.0 firebase_dynamic_links: ^5.3.0
Then run flutter pub get in your terminal to install them.
iOS Setup
- Open your
Info.plist(located inios/Runner/) and add the following entries:- Enable Firebase App Delegate Proxy:
<key>FirebaseAppDelegateProxyEnabled</key> <true/> - Add your app's URL scheme (match the one you set in Firebase Console):
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>your-app-scheme</string> <!-- e.g., myflutterapp --> </array> </dict> </array>
- Enable Firebase App Delegate Proxy:
- Set up Universal Links: In your Firebase Console, go to Dynamic Links > Add URL, enter your domain (like
yourapp.page.link), then follow the steps to associate this domain with your iOS app (you'll need to enable Associated Domains in Xcode and ensure the apple-app-site-association file is properly hosted).
Android Setup
- Open
AndroidManifest.xml(inandroid/app/src/main/) and add an intent filter for deep linking inside the<activity>tag that handles your main app entry:<intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <!-- Replace with your Dynamic Links domain --> <data android:host="yourapp.page.link" android:scheme="https"/> <!-- Optional: Add your app's custom scheme --> <data android:host="open" android:scheme="your-app-scheme"/> </intent-filter> - Ensure you've added the
google-services.jsonfile to theandroid/app/directory (downloaded from Firebase Console).
Initialize Firebase
First, initialize Firebase in your main.dart:
import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_dynamic_links/firebase_dynamic_links.dart'; import 'package:flutter/material.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(const MyApp()); }
Handle Incoming Dynamic Links
You need to handle two scenarios:
- Cold start: The user opens the app via the Dynamic Link when the app isn't running.
- Hot start: The user taps the link while the app is already open.
Add this logic in your initState (or use a FutureBuilder):
class MyApp extends StatefulWidget { const MyApp({super.key}); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { @override void initState() { super.initState(); _initDynamicLinks(); } Future<void> _initDynamicLinks() async { // Handle cold start links final PendingDynamicLinkData? initialLink = await FirebaseDynamicLinks.instance.getInitialLink(); if (initialLink != null) { _handleDeepLink(initialLink); } // Handle hot start links FirebaseDynamicLinks.instance.onLink.listen((dynamicLinkData) { _handleDeepLink(dynamicLinkData); }).onError((error) { // Handle errors here print('Dynamic Link error: ${error.message}'); }); } void _handleDeepLink(PendingDynamicLinkData data) { final Uri deepLink = data.link; // Parse the deep link to get your content route/ID // Example: If your link is https://yourapp.page.link/product?pid=123 if (deepLink.path.contains('/product')) { final String? productId = deepLink.queryParameters['pid']; if (productId != null) { // Navigate to your product detail page Navigator.push( context, MaterialPageRoute( builder: (context) => ProductDetailPage(productId: productId), ), ); } } } @override Widget build(BuildContext context) { return MaterialApp( title: 'Dynamic Links Demo', home: const HomePage(), ); } } // Example Product Detail Page class ProductDetailPage extends StatelessWidget { final String productId; const ProductDetailPage({super.key, required this.productId}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Product Detail')), body: Center(child: Text('Product ID: $productId')), ); } }
Optional: Create Dynamic Links (for sharing)
If you want to generate Dynamic Links within your app (e.g., for users to share content), use this code:
Future<String> createDynamicLink(String productId) async { final DynamicLinkParameters parameters = DynamicLinkParameters( uriPrefix: 'https://yourapp.page.link', link: Uri.parse('https://yourapp.com/product?pid=$productId'), androidParameters: const AndroidParameters( packageName: 'com.yourcompany.yourapp', minimumVersion: 1, ), iosParameters: const IOSParameters( bundleId: 'com.yourcompany.yourapp', minimumVersion: '1.0', ), ); final ShortDynamicLink shortLink = await parameters.buildShortLink(); return shortLink.shortUrl.toString(); }
That's it! Once set up, users tapping your Dynamic Links will be redirected directly to the corresponding content in your Flutter app—whether they have the app installed already (direct deep link) or need to download it first (the link will redirect to the app store and then open the content post-install).
内容的提问来源于stack exchange,提问作者David

