Skip to content

Instantly share code, notes, and snippets.

@inslayn
Last active September 13, 2023 01:13
Show Gist options
  • Save inslayn/8d7e35dae957a97680e02c7e03c20568 to your computer and use it in GitHub Desktop.
Save inslayn/8d7e35dae957a97680e02c7e03c20568 to your computer and use it in GitHub Desktop.
How to get the current web page url in a custom action (plus get route as a bonus)
// ----------------------- Action 1 ------------------------------
import 'dart:html' as html;
import 'package:flutter/src/foundation/constants.dart';
/// Gets the current URL of the current page of the application.
///
/// On web platforms, it returns the current URL.
///
/// On other platforms (e.g., mobile apps with deep linking), an empty string.
Future<String> getCurrentRouteUrl() {
if (kIsWeb) {
// On web, return the current URL.
return Future.value(html.window.location.href);
} else {
return Future.value('');
}
}
// --------------- Action 2 ---------------------------------------------
import 'package:go_router/go_router.dart';
/// Gets the current route of the current page of the application.
///
/// On all platforms, it returns the current route.
Future<String?> getCurrentRoute(BuildContext context) async {
return GoRouter.of(context).location;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment