flux_dynamic_link 1.2.1 copy "flux_dynamic_link: ^1.2.1" to clipboard
flux_dynamic_link: ^1.2.1 copied to clipboard

Flutter plugin for Flux Dynamic Link

example/lib/main.dart

// ignore_for_file: avoid_print

import 'dart:convert';
import 'dart:developer';

import 'package:flutter/material.dart';

import 'package:flux_dynamic_link/flux_dynamic_link.dart';
import 'package:flutter/services.dart';

Future<Map<String, dynamic>> _readCredentials() async {
  try {
    final content = await rootBundle.loadString('assets/credentials.json');
    return json.decode(content) as Map<String, dynamic>;
  } catch (e) {
    log('Failed to read credentials: $e');
    return {};
  }
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final credentials = await _readCredentials();
  if (credentials.isEmpty) {
    log('No credentials found');
    return;
  }

  // Set callback for install referrer detection BEFORE initialize
  FluxDynamicLink.setOnInstallReferrerDetected((result) {
    log('Install Referrer Detected!');
    log('Platform: ${result.platform}');
    log('Match Type: ${result.matchType}');
    log('Deep Link: ${result.deepLink}');
    log('Session ID: ${result.sessionId}');
    log('Metadata: ${result.metadata}');
    
    // You can use this to show a welcome screen, navigate to a specific page, etc.
    // For example, if you want to apply a coupon from the metadata:
    if (result.metadata != null && result.metadata!['coupon'] != null) {
      log('Applying coupon: ${result.metadata!['coupon']}');
    }
  });

  await FluxDynamicLink.initialize(
    publicKey: credentials['public_key']!,
    linkDomain: credentials['link_domain']!,
  );

  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String? _linkValue;
  InstallReferrerResult? _installReferrerResult;

  @override
  void initState() {
    super.initState();
    // Listen to the stream and update the state
    FluxDynamicLink.instance.dynamicLinkStream.listen((value) {
      setState(() {
        _linkValue = value;
        print("Received link value: $value"); // Debug log
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Flux Dynamic Link Example')),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text(
                'Dynamic Link Status',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 8),
              Card(
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('Deep Link: ${_linkValue ?? "Not received yet"}'),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 24),
              const Text(
                'Install Referrer Info',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 8),
              if (_installReferrerResult != null)
                Card(
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('Platform: ${_installReferrerResult!.platform}'),
                        Text('Match Type: ${_installReferrerResult!.matchType}'),
                        Text('Session ID: ${_installReferrerResult!.sessionId ?? "N/A"}'),
                        Text('Matched: ${_installReferrerResult!.matched}'),
                      ],
                    ),
                  ),
                )
              else
                const Card(
                  child: Padding(
                    padding: EdgeInsets.all(16.0),
                    child: Text('No install referrer data (this is normal if you\'ve opened the app before)'),
                  ),
                ),
              const Spacer(),
              Center(
                child: ElevatedButton(
                  onPressed: () {
                    print("Current link value: $_linkValue");
                  },
                  child: const Text('Print Debug Info'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}