dart 我们如何使用Get X来处理深度链接,以便转到应用程序的自定义页面?

1qczuiv0  于 2024-01-04  发布在  其他
关注(0)|答案(2)|浏览(181)

我们如何使用Get X来处理深度链接,以便转到应用程序的自定义页面?
默认情况下,通过将所需地址添加到Android Manifest文件:

  1. <meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
  2. <intent-filter android:autoVerify="true">
  3. <action android:name="android.intent.action.VIEW" />
  4. <category android:name="android.intent.category.DEFAULT" />
  5. <category android:name="android.intent.category.BROWSABLE" />
  6. <data android:scheme="http" android:host="flutterbooksample.com" />
  7. <data android:scheme="https" android:host="flutterbooksample.com"/>
  8. </intent-filter>

字符串
当应用程式开启时,应用程式的主页面会显示给我们。
我期待实现这一点,我可以直接用户到应用程序的任何需要的页面。一个实际的例子是在网页上支付,并返回到应用程序。当我们返回时,我们应该向用户显示有关付款状态的消息,而不是直接用户到应用程序的第一页。

uqdfh47h

uqdfh47h1#

我的方法和Didier的答案类似,但是我在singleTon类中使用了这个,在main.dart中调用(在返回MyApp行上方)

  1. await DeepLinkService().initialDeepLinks();

字符串
但我不知道如何取消和处置StreamSubscription

  1. StreamSubscription? _sub;
  2. Future<void> initialDeepLinks() async {
  3. try {
  4. //init deeplink when start from inactive
  5. final initialLink = await getInitialLink();
  6. if (initialLink != null) {
  7. await _handleDeepLink(initialLink);
  8. }
  9. //Check deeplink in foreground/background
  10. _sub = linkStream.listen((String? link) async {
  11. if (link != null) {
  12. await _handleDeepLink(link);
  13. }
  14. }, onError: (e) {
  15. DebugLog().show(e.toString());
  16. });
  17. } catch (e) {
  18. DebugLog().show(e.toString());
  19. }
  20. }
  21. // Handle the deep link
  22. Future<void> _handleDeepLink(String link) async {
  23. Uri deepLink = Uri.parse(link);
  24. String path = deepLink.path;
  25. DebugLog().show('open app from deeplink: $deepLink with path: $path');
  26. //Switch the path then navigate to destinate page
  27. }

展开查看全部
6jjcrrmo

6jjcrrmo2#

当你处理深度链接时,在你的应用程序中进行页面路由。
在我的应用程序中,我通常使用uni_links(https://pub.dev/packages/uni_links),在我的main.dart中,我有这样的东西:

  1. StreamSubscription<String> _subUniLinks;
  2. @override
  3. initState() {
  4. super.initState();
  5. // universal link setup
  6. initUniLinks();
  7. }
  8. Future<void> initUniLinks() async {
  9. try {
  10. final String initialLink = await getInitialLink();
  11. await processLink(initialLink);
  12. } on PlatformException {
  13. }
  14. _subUniLinks = linkStream.listen(processLink, onError: (err) {});
  15. }
  16. processLink(String link) async {
  17. // parse link and decide what to do:
  18. // - use setState
  19. // - or use Navigator.pushReplacement
  20. // - or whatever mechanism if you have in your app for routing to a page
  21. }
  22. @override
  23. dispose() {
  24. if (_subUniLinks != null) _subUniLinks.cancel();
  25. super.dispose();
  26. }

字符串

展开查看全部

相关问题