legato_centralize_deep_link 0.0.1 legato_centralize_deep_link: ^0.0.1 copied to clipboard
Centralize deep link
import 'dart:convert';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:deep_link/deep_link.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _deepLink = 'Unknown';
String _metaData = 'Unknown';
final _deepLinkPlugin = DeepLink();
final _config = const DeepLinkConfiguration(
projectId: '6c9982c4-aa53-422e-9438-c7c76fda7672',
apiKey: 'VZE@C9j70rR132EtecVWFP19IbF93ATNOSvkNxqnS0sxrDDOb5',
secret: 'hTkgoz59ryKpbS@5DUOH!g6XncigIXrJ\$YoB9WXl\$Us\$OQeCyy');
final _env = DeepLinkEnvironment.dev;
@override
void initState() {
super.initState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
FilledButton(
onPressed: () {
_generateDeepLink();
},
child: const Text('Generate deep link')),
Text('Deep link = $_deepLink'),
FilledButton(
onPressed: () {
_extraMetadata();
},
child: const Text('Extract meta data deep link')),
Text('Meta data = $_metaData'),
],
),
),
),
);
}
Future<void> _generateDeepLink() async {
String generateLink;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
generateLink = await _deepLinkPlugin.generateDeepLink(
_config,
_env,
const GenerateLinkRequest(metadata: {
"page_type": "product",
"page_id": 1,
"page_title": "Aliquam erat volutpat."
}, pageUrl: 'https://placeholder.com/'));
} on PlatformException {
generateLink = 'Failed to generate deep link';
} on Exception {
generateLink = 'On Exception';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_deepLink = generateLink;
});
}
Future<void> _extraMetadata() async {
String token = 'pOLhcR9XF8JoWEi36';
Map<String, dynamic> metaData = {};
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
final metaRequest = {
'is_click': false,
'is_redirect': true,
'is_flutter': true
};
metaData = await _deepLinkPlugin.extractLinkMetaData(_config, _env, token,
metaData: metaRequest);
} on PlatformException {
_metaData = 'Failed to generate deep link';
} on Exception {
_metaData = 'On Exception';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_metaData = jsonEncode(metaData);
});
}
}