vela_links 0.2.5
vela_links: ^0.2.5 copied to clipboard
Deep links, deferred deep links, short links, QR codes, link tracking and in-app event analytics (sessions, screens, sign-ups, purchases) for Flutter.
example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:vela_links/vela_links.dart';
/// The only in-app screens a link may open. Anything else is ignored: a link is untrusted input.
const allowedRoutes = {'/', '/invite', '/product'};
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Replace with the SDK key from your Vela Links dashboard.
await VelaLinks.init(
apiKey: 'vl_your_sdk_key', baseUrl: 'https://api.links.velaapps.in');
runApp(const ExampleApp());
}
/// A small app that shows the last link it was opened with, and can create a new one.
class ExampleApp extends StatefulWidget {
/// Creates the example app.
const ExampleApp({super.key});
@override
State<ExampleApp> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
String _status = 'Open a link to see it here';
String? _created;
StreamSubscription<VelaLink>? _subscription;
@override
void initState() {
super.initState();
// A link that arrived before this screen started listening.
final early = VelaLinks.instance.consumePendingLink();
if (early != null) _show(early);
_subscription = VelaLinks.instance.onLink.listen(_show);
}
void _show(VelaLink link) {
final opened = allowedRoutes.contains(link.route)
? 'Opened ${link.route} ${link.params}${link.deferred ? ' (after install)' : ''}'
: 'Ignored a link to an unknown screen';
if (mounted) setState(() => _status = opened);
}
Future<void> _create() async {
String text;
try {
text = await VelaLinks.instance
.createLink(route: '/invite', params: {'id': '42'});
} on VelaLinksException catch (e) {
text = 'Could not create a link: ${e.message}';
}
if (mounted) setState(() => _created = text);
}
@override
void dispose() {
unawaited(_subscription?.cancel());
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorObservers: [
VelaLinksObserver()
], // reports each named screen to the Behaviour dashboard
home: Scaffold(
appBar: AppBar(title: const Text('Vela Links example')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(_status),
const SizedBox(height: 16),
FilledButton(
onPressed: _create, child: const Text('Create a link')),
TextButton(
onPressed: () => VelaLinks.instance.trackPurchase(499),
child: const Text('Pretend to buy (tracks a purchase)')),
if (_created != null) SelectableText(_created!),
],
),
),
),
);
}
}