3

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');
    }
  }
}
yash soni
  • 31
  • 2

1 Answers1

0

To redirect the WebView dynamically, you will need to get the WebViewController variable after the WebView loaded. Then call .loadUrl(newUrl);

For example you create the _webControl variable outside the build function first:

late WebViewController _webControl;

@override
Widget build(BuildContext ctx) {
...

Then assign the _webControl inside the WebView after it created:

WebView(
    initialUrl: firstTimeURL,
    javascriptMode: JavascriptMode.unrestricted,
    onWebViewCreated: (WebViewController webViewController) {
      _webControl=webViewController;
      webViewController.evaluateJavascript('''alert("Henlo from flutter")''');
    },
),

Finnaly call .loadUrl(xxx) whenever you want (inside a material button,...). No need to setState:

_webControl.loadUrl(temp);
phnghue
  • 1,578
  • 2
  • 10
  • 9