How To Get Current Url Using Flutter WebView Android App

admin_img Posted By Bajarangi soft , Posted On 07-11-2020

Working with Flutter webview can be easy but in some situations you might what to get the display web page url. Consider when you want to make sure that the loaded url is from a particular domain.We are going to use flutter webview community plugin. If you have done this using default flutter webview and consider sharing your code kindly use the comment below.

How To Get Current Url Using Flutter WebView Android App

Current Url
Step 1 
We cannot directly remove the time stamp from Current Url but using the intl.dart package we can easily filter the date stamp from time stamp. So open your flutter project’s pubspec.yaml in code .

dependencies:
  flutter:
    sdk: flutter
  webview_flutter: 0.2.0
  flutter_webview_plugin: 0.3.0+2

Step 2
After done saving the pubspec.yaml file, Open your flutter project root folder in Command Prompt or Terminal and execute flutter pub get command. 

flutter pub get



Complete Code For  Current Url In Flutter
main.dart
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final flutterWebviewPlugin = new FlutterWebviewPlugin();
  GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

  StreamSubscription<String> _onUrlChanged;

  @override
  void initState() {
    super.initState();

    _onUrlChanged = flutterWebviewPlugin.onUrlChanged.listen((String url) {
      if (mounted) {
        print("Current URL: $url");
      }
    });
  }

  @override
  void dispose() {
    _onUrlChanged.cancel();
    flutterWebviewPlugin.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WebviewScaffold(
      key: scaffoldKey,
      url: 'https://bajarangisoft.com',
      hidden: true,
      appBar: AppBar(
        backgroundColor: Colors.indigo,
          title: Text("Current Url")),
    );
  }
}

Related Post