在这里,我试图在 webview 中加载深层链接首先在 google 中输入深层链接并将其重定向到应用程序并在 webview 中加载深层链接。如果我把静态 URL 像 var temp = "www.google.com" 然后使用 temp var 作为 webview 中的初始 URL,它将加载谷歌应用内。但我试图将深层链接 var 复制到字符串 var 中,并在 webview 中使用 temp var 作为初始 URL,它不会在 webview 中加载动态链接。甚至我将深层链接 var 转换为字符串(将 URI 类型转换为字符串)
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"; // 这将作为 webview 中的初始 url 正常工作
@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');
}
}
}