linkhopp 1.1.0
linkhopp: ^1.1.0 copied to clipboard
Deferred deep links for Flutter. Token-based, no device fingerprinting, flat price per app, contract with a German company. Android and iOS.
example/lib/main.dart
import 'dart:io' show Platform;
// ignore: deprecated_member_use
import 'package:flutter/material.dart';
import 'package:linkhopp/linkhopp.dart';
/// Pass the project's app key at launch:
/// flutter run --dart-define=LINKHOPP_API_KEY=your_app_key
const _apiKey = String.fromEnvironment('LINKHOPP_API_KEY');
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
if (_apiKey.isNotEmpty) {
await LinkHopp.init(apiKey: _apiKey);
}
runApp(const ExampleApp());
}
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key});
@override
Widget build(BuildContext context) => MaterialApp(
title: 'LinkHopp Example',
theme: ThemeData(colorSchemeSeed: Colors.indigo, useMaterial3: true),
home: const HomePage(),
);
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final _log = <String>[];
bool get _ready => _apiKey.isNotEmpty;
@override
void initState() {
super.initState();
if (!_ready) {
_add('No API key. Start with --dart-define=LINKHOPP_API_KEY=...');
return;
}
_start();
}
Future<void> _start() async {
var receivedDirectLink = false;
LinkHopp.handleLink((link) {
receivedDirectLink = true;
_showLink('Incoming link', link);
});
final initial = await LinkHopp.getInitialLink();
if (initial != null) {
receivedDirectLink = true;
_showLink('Start link', initial);
}
// Freshly installed? On Android the SDK reads the Play Install
// Referrer, without the clipboard. On iOS nothing happens here, see
// the button below.
if (mounted && !receivedDirectLink && Platform.isAndroid) {
await _checkDeferred();
}
}
Future<void> _checkDeferred({bool allowClipboard = false}) async {
final result =
await LinkHopp.checkDeferredLinkResult(allowClipboard: allowClipboard);
if (result.matched) {
_showLink('Deferred link', result.link!);
} else {
// The status says WHY there was no match.
_add('No match: ${result.status.name} - ${result.message}');
}
}
void _showLink(String source, LinkHoppLink link) {
_add('$source: ${link.destinationUrl} (${link.matchMethod})');
// Use link.path for in-app routing and link.metadata for application data.
}
void _add(String line) {
if (!mounted) return;
setState(() => _log.insert(0, line));
}
@override
void dispose() {
LinkHopp.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('LinkHopp Example')),
body: Column(
children: [
if (_ready && Platform.isIOS)
Padding(
padding: const EdgeInsets.all(16),
child: FilledButton(
// Explicit user action: only now may the SDK read the
// clipboard. iOS may ask on its own.
onPressed: () => _checkDeferred(allowClipboard: true),
child: const Text('Accept invitation'),
),
),
Expanded(
child: ListView(
padding: const EdgeInsets.symmetric(horizontal: 16),
children: [for (final l in _log) ListTile(title: Text(l))],
),
),
],
),
);
}