Last active
September 13, 2023 01:13
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------------------- 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