Here I am trying to load deep link in webview first enter the deep link in google and its redirect to the app and load deep link in webview. if I put static URL like var temp = "www.google.com" and then used temp var as initial URL in webview it will load the google in-app. but I tried to copy deep link var into string var and used temp var as initial URL in webview it will no load dynamic link in webview. even I converted deep link var into a string (typecast URI to string)
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(MaterialApp(
title: 'Dynamic Links Example',
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => _MainScreen(),
},
));
}
class _MainScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MainScreenState();
}
class _MainScreenState extends State<_MainScreen> {
String temp;
// String temp = "www.google.com"; // this will work correct as intial url in webview
@override
void initState() {
initDynamicLinks();
super.initState();
}
Future initDynamicLinks() async {
// 1. Get the initial dynamic link if the app is opened with a dynamic link
final PendingDynamicLinkData data =
await FirebaseDynamicLinks.instance.getInitialLink();
// 2. handle link that has been retrieved
_handleDeepLink(data);
// 3. Register a link callback to fire if the app is opened up from the background
// using a dynamic link.
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData dynamicLink) async {
// 3a. handle link that has been retrieved
_handleDeepLink(dynamicLink);
}, onError: (OnLinkErrorException e) async {
print('Link Failed: ${e.message}');
});
}
@override
Widget build(BuildContext context) {
return Material(
child: Scaffold(
appBar: AppBar(
title: const Text('Dynamic Links Example'),
),
body: Builder(builder: (BuildContext context) {
return Center(
child: WebView(
initialUrl: temp, // dynamic url copy from deep link var it will not load in web view
javascriptMode: JavascriptMode.unrestricted,
),
);
}),
),
);
}
void _handleDeepLink(PendingDynamicLinkData data) {
final Uri deepLink = data?.link;
if (deepLink != null) {
temp = deepLink.toString(); // here i had done type casting to uri to string
print('_handleDeepLink | deeplink: $deepLink');
}
}
}