How to do deep linking in flutter without firebase?
Asked Answered
J

1

11

I have got a Netflix model app and I want to share the movie contents to others, and if the receiver has that app, then the content should open up in his/her app or if app not present, got to the website.

I tried with Firebase, but it didn't go well, so I tried with uni_links in flutter but its my first time using it and didn't find much of the tutorials about it.

Below is my code, its purely in flutter and used the share widget to share the contents.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:share/share.dart';
import 'package:uni_links/uni_links.dart';

// Share tab
enum UniLinksType { string, uri }

class SharePage extends StatelessWidget 
{
    SharePage(this.shareType, this.share, this.image);
    final shareType;
    final VideoDataModel share;
    final image;

    String latestLink;
    Uri latestUri;

    StreamSubscription sub;
    UniLinksType type = UniLinksType.string;

    initPlatformState() async
    {

        if(type == UniLinksType.string)
        {
            await initPlatformStateForStringUniLinks();
        }
        else
        {
            await initPlatformStateForUriUniLinks();
        }
    }

    initPlatformStateForStringUniLinks() async
    {
        sub = getLinksStream().listen((String link)
        {
            latestLink = link ?? "Unknown";
            latestUri = null;

            try
            {
                if(link != null)
                {
                    latestUri = Uri.parse(link);
                }
            }
            on FormatException{}
        },
        onError: (err)
        {
            latestLink = "Failed to load latest link: $err";
            latestUri = null;
        });

        getLinksStream().listen((String link)
        {
            print('got link: $link');
        },
        onError: (err)
        {
            print('got err: $err');
        });

        String initialLink;
        Uri initialUri;

        // Platform messages may fail, so we use a try/catch PlatformException.
        try
        {
            initialLink = await getInitialLink();
            print("initial link: $initialLink");

            if (initialLink != null)
            {
                print("in if");
                initialUri = Uri.parse(initialLink);
            }
        }
        on PlatformException
        {
            initialLink = 'Failed to get initial link.';
            initialUri = null;
        }
        on FormatException
        {
            initialLink = 'Failed to parse the initial link as Uri.';
            initialUri = null;
        }
    }

    Future initPlatformStateForUriUniLinks() async
    {
        sub = getUriLinksStream().listen((Uri uri)
        {
            latestUri = uri;
            latestLink = uri?.toString() ?? 'Unknown';
        },
        onError: (err)
        {
            latestUri = null;
            latestLink = 'Failed to get latest link: $err.';
        });

        getUriLinksStream().listen((Uri uri)
        {
            print('got uri: ${uri?.path} ${uri?.queryParametersAll}');
        },
        onError: (err)
        {
            print('got err: $err');
        });

        Uri initialUri;
        String initialLink;

        try
        {
            initialUri = await getInitialUri();
            print('initial uri: ${initialUri?.path}'' ${initialUri?.queryParametersAll}');
            initialLink = initialUri?.toString();
        }
        on PlatformException
        {
            initialUri = null;
            initialLink = 'Failed to get initial uri.';
        }
        on FormatException
        {
            initialUri = null;
            initialLink = 'Bad parse the initial link as Uri.';
        }
        latestUri = initialUri;
        latestLink = initialLink;

        return latestUri;
    }

    Widget shareText()
    {
        return Text
        (
            "Share",
            style: TextStyle
            (
                fontFamily: 'Lato',
                fontSize: 12.0,
                fontWeight: FontWeight.w600,
                letterSpacing: 0.0,
                color: Colors.white
            ),
        );
    }

    Widget shareTabColumn()
    {
        return Column
        (
            mainAxisAlignment:
            MainAxisAlignment.center,
            children: <Widget>
            [
                Icon
                (
                    Icons.share,
                    size: 30.0,
                    color: Colors.white,
                ),
                new Padding
                (
                    padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 10.0),
                ),
                shareText(),
            ],
        );
    }

    @override
    Widget build(BuildContext context) 
    {
        return Expanded
        (
            child: Material
            (
                child:  new InkWell
                (
                    onTap: ()
                    {
                       Share.share('$shareType'+ share.id.toString() + "\n" + share.name);
                       initPlatformState();
                    },
                    child: shareTabColumn(),
                ),
                color: Colors.transparent,
            )
        );
    }
}

In the sharing content I want to get my domain name (say for example www.example.co.in)

Jaquith answered 6/10, 2020 at 8:14 Comment(0)
S
4

You don't need firebase for deeplinking. You only need follow the official docs to enable deeplinking on both iOS and Android here Deep linking

for the sake of completeness here a snippet to enable deeplinking on Android and IOS from the docs.

AndroidManifest.xml

<meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="http" android:host="flutterbooksample.com" />
    <data android:scheme="https" />
</intent-filter>

iOS Info.plist

<key>FlutterDeepLinkingEnabled</key>
<true/>
<key>CFBundleURLTypes</key>
<array>
    <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLName</key>
    <string>flutterbooksample.com</string>
    <key>CFBundleURLSchemes</key>
    <array>
    <string>customscheme</string>
    </array>
    </dict>
</array>

And then use the url_launcher package to open the url.

Sexagenary answered 28/10, 2022 at 4:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.