bit_2_connect_sdk 1.0.0 copy "bit_2_connect_sdk: ^1.0.0" to clipboard
bit_2_connect_sdk: ^1.0.0 copied to clipboard

A Flutter SDK for handling deferred deep linking and dynamic link creation with cross-platform support for iOS and Android.

example/lib/main.dart

import 'package:bit_2_connect_sdk/bit_2_connect_sdk.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Bit2Connect SDK Example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Bit2Connect SDK Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final Bit2ConnectSDK _sdk = Bit2ConnectSDK.instance;
  String _status = 'Initializing...';
  String _deferredLinkResult = '';
  String _createdLink = '';

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

  void _initializeSDK() {
    // Initialize the SDK with your configuration
    _sdk.initialize(
      baseUrl: 'https://api.bit2connect.com', // Replace with your API base URL
      apiKey: 'your-api-key', // Replace with your API key
      debug: true, // Enable debug logging
    );

    setState(() {
      _status = 'SDK Initialized';
    });

    // Handle deferred deep link on app launch
    _handleDeferredDeepLink();
  }

  Future<void> _handleDeferredDeepLink() async {
    try {
      final result = await _sdk.handleDeferredDeepLink();

      setState(() {
        _deferredLinkResult = result.toString();
      });

      // Handle the result using extension methods
      result.onSuccess((success) {
        if (mounted) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('Deferred link matched: ${success.deepLink}'),
              backgroundColor: Colors.green,
            ),
          );
        }
      });

      result.onError((error) {
        if (mounted) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('Deferred link error: $error'),
              backgroundColor: Colors.red,
            ),
          );
        }
      });

      result.onNoMatch(() {
        if (mounted) {
          ScaffoldMessenger.of(context).showSnackBar(
            const SnackBar(
              content: Text('No deferred link found'),
              backgroundColor: Colors.orange,
            ),
          );
        }
      });
    } catch (e) {
      setState(() {
        _deferredLinkResult = 'Error: $e';
      });
    }
  }

  Future<void> _createDynamicLink() async {
    try {
      final result = await _sdk.createDynamicLink(
        deepLink:
            'myapp://yourapp.com/campaign/123', // Replace with your app scheme
        androidPackage: 'com.yourapp',
        androidFallbackUrl:
            'https://play.google.com/store/apps/details?id=com.yourapp',
        iosBundleId: 'com.yourapp',
        iosFallbackUrl: 'https://apps.apple.com/app/yourapp/id123456789',
        iosAppStoreId: '123456789',
        desktopFallbackUrl: 'https://yourapp.com',
        campaignParams: {
          'campaign': 'example',
          'utm_source': 'mobile_app',
          'utm_medium': 'dynamic_link',
        },
        customData: {'user_id': '12345', 'campaign_id': '67890'},
      );

      if (result is CreateLinkSuccess) {
        setState(() {
          _createdLink = result.shortUrl;
        });

        if (mounted) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('Link created: ${result.shortUrl}'),
              backgroundColor: Colors.green,
            ),
          );
        }
      } else if (result is CreateLinkError) {
        if (mounted) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('Error creating link: ${result.message}'),
              backgroundColor: Colors.red,
            ),
          );
        }
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Exception: $e'), backgroundColor: Colors.red),
        );
      }
    }
  }

  void _testDirectLink() {
    const testLink = 'https://b2co.link/abc123?click_id=xyz789&param1=value1';
    final result = _sdk.handleDirectDeepLink(testLink);

    if (result is DirectLinkSuccess) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('Direct link parsed: ${result.url}'),
          backgroundColor: Colors.blue,
        ),
      );
    } else {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content: Text('No direct link found'),
          backgroundColor: Colors.grey,
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'SDK Status',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
                    const SizedBox(height: 8),
                    Text(_status),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Deferred Link Result',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
                    const SizedBox(height: 8),
                    Text(
                      _deferredLinkResult.isEmpty
                          ? 'No result yet'
                          : _deferredLinkResult,
                      style: const TextStyle(fontFamily: 'monospace'),
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Created Link',
                      style: Theme.of(context).textTheme.headlineSmall,
                    ),
                    const SizedBox(height: 8),
                    Text(
                      _createdLink.isEmpty
                          ? 'No link created yet'
                          : _createdLink,
                      style: const TextStyle(fontFamily: 'monospace'),
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 24),
            ElevatedButton(
              onPressed: _createDynamicLink,
              child: const Text('Create Dynamic Link'),
            ),
            const SizedBox(height: 8),
            ElevatedButton(
              onPressed: _testDirectLink,
              child: const Text('Test Direct Link Parsing'),
            ),
            const SizedBox(height: 8),
            ElevatedButton(
              onPressed: _handleDeferredDeepLink,
              child: const Text('Check Deferred Link Again'),
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
160
points
191
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter SDK for handling deferred deep linking and dynamic link creation with cross-platform support for iOS and Android.

Homepage
Repository (GitHub)
View/report issues

Topics

#deep-linking #dynamic-links #deferred-deep-linking #attribution #mobile-marketing

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

device_info_plus, flutter, flutter_secure_storage, http

More

Packages that depend on bit_2_connect_sdk