Deep Link in Flutter
Deep linking in Flutter allows a URL to open your app to a specific screen or piece of content, improving user experience and engagement. The recommended approach is to use standard web URLs (HTTPS) with platform-specific configurations known as App Links (Android) and Universal Links (iOS).
In deep linking application may already be open when the link is clicked this means we need to handle link clicks in the background of a running application. Let’s see how to do this best in Flutter. It’s necessary to configure appropriate permissions for the deep linking. Permissions are configured in exactly the same way as the corresponding native configurations. For Android (Manifest.xml ) For Ios (Info.plist)
Main Dart FIle Call (Let My Example I did for the url )
A New Appointment Request.TA250051 received from Vendor Please consider for Approval/Reject.Visit https://www.xxxxx.xxxx/cmVxdWVzdD9pZD1UQTI1MDA1MS0w for details.
Delcare in pubspec
app_links: ^6.4.1 # Latest stable
void _handleDeepLink(Uri uri, [BuildContext? context]) {
print("Received Deep Link: $uri");
if (uri.scheme == "https" &&
uri.host == "www.xxxxx.xxx" &&
uri.pathSegments.isNotEmpty) {
// This is EXACTLY what Java's getLastPathSegment() does
final String encodedPath = uri.pathSegments.last;
print("Encoded Path (last segment): $encodedPath");
final currentContext = navigatorKey.currentContext;
if (currentContext != null) {
Navigator.push(
currentContext,
MaterialPageRoute(
builder: (context) => DeepLinkPassScreen(encodedPath: encodedPath),
),
);
}
}
}
And in DeepLinkPassScreen, use this bulletproof decoder (same as Java)
Future<void> _processDeepLink() async {
try {
String encoded = widget.encodedPath.trim();
print("Raw encoded from URL: $encoded"); // e.g., UQTI1MDAwNDAtMA==
// Java's decodeBase64() equivalent — handles padding automatically
final decodedBytes = base64Decode(encoded);
final decodedString = utf8.decode(decodedBytes);
print("Decoded successfully: $decodedString"); // id=TA250040-0
if (decodedString.contains("id=")) {
final idPart = decodedString.split("id=")[1];
final parts = idPart.split("-");
if (parts.length >= 2) {
reqNo = parts[0].trim();
flag = parts[1].trim();
print("REQ_NO: $reqNo, FLAG: $flag");
await _validateLink();
} else {
_showError("Invalid format");
}
} else {
_showError("No id found");
}
} catch (e) {
print("Decode failed: $e");
_showError("Invalid or corrupted link");
}
}
With these configurations and code in place clicking the “Open URL” button will launch the URL specified on both Android and iOS devices. Make sure to handle the deep link in your app’s code accordingly.
Post a Comment
0Comments