Skip to content

Commit 0c43714

Browse files
authored
adds snippets for dynamic links and ml downloader (#9)
Co-authored-by: Eric Windmill <[email protected]>
1 parent 982f33e commit 0c43714

File tree

6 files changed

+232
-1
lines changed

6 files changed

+232
-1
lines changed

packages/firebase_snippets_app/lib/app.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
import 'package:cloud_firestore/cloud_firestore.dart';
16+
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
1617
import 'package:firebase_remote_config/firebase_remote_config.dart';
1718
import 'package:firebase_snippets_app/snippets/firestore.dart';
1819
import 'package:firebase_snippets_app/snippets/remote_config.dart';
@@ -23,10 +24,12 @@ class MyApp extends StatefulWidget {
2324
Key? key,
2425
required this.firestore,
2526
required this.firebaseRemoteConfig,
27+
this.initialLink,
2628
}) : super(key: key);
2729

2830
final FirebaseFirestore firestore;
2931
final FirebaseRemoteConfig firebaseRemoteConfig;
32+
final PendingDynamicLinkData? initialLink;
3033

3134
@override
3235
State<MyApp> createState() => _MyAppState();
@@ -53,7 +56,40 @@ class _MyAppState extends State<MyApp> {
5356
appBar: AppBar(
5457
title: const Text('Snippet Test'),
5558
),
59+
body: HomePage(
60+
initialLink: widget.initialLink,
61+
),
5662
),
5763
);
5864
}
5965
}
66+
67+
class HomePage extends StatelessWidget {
68+
const HomePage({
69+
Key? key,
70+
this.initialLink,
71+
}) : super(key: key);
72+
73+
final PendingDynamicLinkData? initialLink;
74+
75+
@override
76+
Widget build(BuildContext context) {
77+
// [START receive_dynamic_link_handle_initial_link]
78+
if (initialLink != null) {
79+
final Uri? deepLink = initialLink!.link;
80+
// Example of using the dynamic link to push the user to a different screen
81+
Navigator.pushNamed(context, deepLink!.path);
82+
}
83+
// [END receive_dynamic_link_handle_initial_link]
84+
85+
// [START receive_dynamic_link_listen_in_background]
86+
FirebaseDynamicLinks.instance.onLink.listen((dynamicLinkData) {
87+
Navigator.pushNamed(context, dynamicLinkData.link.path);
88+
}).onError((error) {
89+
// Handle errors
90+
});
91+
// [END receive_dynamic_link_listen_in_background]
92+
93+
return Container();
94+
}
95+
}

packages/firebase_snippets_app/lib/main.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import 'package:cloud_firestore/cloud_firestore.dart';
1717
import 'package:firebase_auth/firebase_auth.dart';
1818
import 'package:firebase_core/firebase_core.dart';
19+
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
1920
import 'package:firebase_remote_config/firebase_remote_config.dart';
2021
// [END set_up_environment]
2122

@@ -48,6 +49,7 @@ void main() async {
4849
);
4950

5051
final db = FirebaseFirestore.instance;
52+
5153
// [START access_data_offline_configure_offline_persistence]
5254
final settings = db.settings.copyWith(persistenceEnabled: true);
5355
// [END access_data_offline_configure_offline_persistence]
@@ -62,6 +64,11 @@ void main() async {
6264
FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.instance;
6365
// [END get_started_get_singleton_object]
6466

67+
// [START dynamic_links_get_initial_links]
68+
final PendingDynamicLinkData? initialLink =
69+
await FirebaseDynamicLinks.instance.getInitialLink();
70+
// [END dynamic_links_get_initial_links]
71+
6572
if (kIsWeb) {
6673
// [START auth_persistingAuthState]
6774
await FirebaseAuth.instance.setPersistence(Persistence.NONE);
@@ -74,6 +81,7 @@ void main() async {
7481
MyApp(
7582
firestore: db,
7683
firebaseRemoteConfig: firebaseRemoteConfig,
84+
initialLink: null,
7785
),
7886
);
7987
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// ignore_for_file: non_constant_identifier_names
2+
3+
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
4+
import 'package:firebase_snippets_app/snippets/snippet_base.dart';
5+
6+
class DynamicLinkSnippets implements DocSnippet {
7+
@override
8+
void runAll() {
9+
// TODO: implement runAll
10+
}
11+
12+
void createDynamicLinks_createParams() async {
13+
// [START create_dynamic_links_create_params]
14+
final dynamicLinkParams = DynamicLinkParameters(
15+
link: Uri.parse("https://www.example.com/"),
16+
uriPrefix: "https://example.page.link",
17+
androidParameters:
18+
const AndroidParameters(packageName: "com.example.app.android"),
19+
iosParameters: const IOSParameters(bundleId: "com.example.app.ios"),
20+
);
21+
final dynamicLink =
22+
await FirebaseDynamicLinks.instance.buildLink(dynamicLinkParams);
23+
// [END create_dynamic_links_create_params]
24+
}
25+
26+
void createDynamicLinks_shortLinks() async {
27+
// [START create_dynamic_links_short_links]
28+
final dynamicLinkParams = DynamicLinkParameters(
29+
link: Uri.parse("https://www.example.com/"),
30+
uriPrefix: "https://example.page.link",
31+
androidParameters:
32+
const AndroidParameters(packageName: "com.example.app.android"),
33+
iosParameters: const IOSParameters(bundleId: "com.example.app.ios"),
34+
);
35+
final dynamicLink =
36+
await FirebaseDynamicLinks.instance.buildShortLink(dynamicLinkParams);
37+
// [END create_dynamic_links_short_links]
38+
39+
// [START create_dynamic_links_unguessable]
40+
final unguessableDynamicLink =
41+
await FirebaseDynamicLinks.instance.buildShortLink(
42+
dynamicLinkParams,
43+
shortLinkType: ShortDynamicLinkType.unguessable,
44+
);
45+
// [END create_dynamic_links_unguessable]
46+
}
47+
48+
void createDynamicLinks_params() async {
49+
// [START create_dynamic_links_params]
50+
final dynamicLinkParams = DynamicLinkParameters(
51+
link: Uri.parse("https://www.example.com/"),
52+
uriPrefix: "https://example.page.link",
53+
androidParameters: const AndroidParameters(
54+
packageName: "com.example.app.android",
55+
minimumVersion: 30,
56+
),
57+
iosParameters: const IOSParameters(
58+
bundleId: "com.example.app.ios",
59+
appStoreId: "123456789",
60+
minimumVersion: "1.0.1",
61+
),
62+
googleAnalyticsParameters: const GoogleAnalyticsParameters(
63+
source: "twitter",
64+
medium: "social",
65+
campaign: "example-promo",
66+
),
67+
socialMetaTagParameters: SocialMetaTagParameters(
68+
title: "Example of a Dynamic Link",
69+
imageUrl: Uri.parse("https://example.com/image.png"),
70+
),
71+
);
72+
final dynamicLink =
73+
await FirebaseDynamicLinks.instance.buildShortLink(dynamicLinkParams);
74+
// [END create_dynamic_links_params]
75+
}
76+
77+
void receiveDynamicLinks_testExactLink() async {
78+
// [START receive_dynamic_links_test_exact_link]
79+
String link = 'https://dynamic-link-domain/ke2Qa';
80+
81+
final PendingDynamicLinkData? initialLink =
82+
await FirebaseDynamicLinks.instance.getDynamicLink(Uri.parse(link));
83+
// [END receive_dynamic_links_test_exact_link]
84+
}
85+
86+
void receiveDynamicLink_listenInBackground() async {}
87+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// ignore_for_file: non_constant_identifier_names, avoid_print
2+
3+
import 'package:firebase_core/firebase_core.dart';
4+
import 'package:firebase_ml_model_downloader/firebase_ml_model_downloader.dart';
5+
import 'package:firebase_snippets_app/snippets/snippet_base.dart';
6+
7+
class MLModelDownloaderSnippets implements DocSnippet {
8+
@override
9+
void runAll() {
10+
// This plug in is in beta. It won't be tested for now.
11+
}
12+
13+
void mlDownloader_createReference() async {
14+
// [START ml_downloader_create_reference]
15+
FirebaseModelDownloader downloader = FirebaseModelDownloader.instance;
16+
// [END ml_downloader_create_reference]
17+
18+
// [START ml_downloader_secondary_app_reference]
19+
FirebaseApp secondaryApp = Firebase.app('SecondaryApp');
20+
FirebaseModelDownloader secondaryDownloader =
21+
FirebaseModelDownloader.instanceFor(app: secondaryApp);
22+
// [END ml_downloader_secondary_app_reference]
23+
24+
// [START ml_downloader_list_downloaded_models]
25+
List<FirebaseCustomModel> models =
26+
await FirebaseModelDownloader.instance.listDownloadedModels();
27+
// [END ml_downloader_list_downloaded_models]
28+
29+
// [START ml_downloader_custom_model]
30+
List<FirebaseCustomModel> customModels =
31+
await FirebaseModelDownloader.instance.listDownloadedModels();
32+
33+
for (var model in customModels) {
34+
print('Name: ${model.name}');
35+
print('Size: ${model.size}');
36+
print('Hash: ${model.hash}');
37+
}
38+
// [END ml_downloader_custom_model]
39+
40+
// [START ml_downloader_download_model]
41+
FirebaseCustomModel model = await FirebaseModelDownloader.instance
42+
.getModel('myModel', FirebaseModelDownloadType.latestModel);
43+
// [END ml_downloader_download_model]
44+
45+
// [START ml_downloader_conditions]
46+
// The following are the default conditions:
47+
FirebaseModelDownloadConditions conditions =
48+
FirebaseModelDownloadConditions(
49+
// Download whilst connected to cellular data
50+
iosAllowsCellularAccess: true,
51+
// Allow downloading in the background
52+
iosAllowsBackgroundDownloading: false,
53+
// Only download whilst charging
54+
androidChargingRequired: false,
55+
// Only download whilst on Wifi
56+
androidWifiRequired: false,
57+
// Only download whilst the device is idle
58+
androidDeviceIdleRequired: false,
59+
);
60+
61+
FirebaseCustomModel modelWithConditions = await FirebaseModelDownloader
62+
.instance
63+
.getModel('myModel', FirebaseModelDownloadType.latestModel, conditions);
64+
// [END ml_downloader_conditions]
65+
66+
// [START ml_downloader_delete_a_model]
67+
await FirebaseModelDownloader.instance.deleteDownloadedModel('myModel');
68+
// [END ml_downloader_delete_a_model]
69+
}
70+
}

packages/firebase_snippets_app/pubspec.lock

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ packages:
162162
url: "https://pub.dartlang.org"
163163
source: hosted
164164
version: "1.6.1"
165+
firebase_dynamic_links:
166+
dependency: "direct main"
167+
description:
168+
name: firebase_dynamic_links
169+
url: "https://pub.dartlang.org"
170+
source: hosted
171+
version: "4.1.2"
172+
firebase_dynamic_links_platform_interface:
173+
dependency: transitive
174+
description:
175+
name: firebase_dynamic_links_platform_interface
176+
url: "https://pub.dartlang.org"
177+
source: hosted
178+
version: "0.2.2+2"
165179
firebase_messaging:
166180
dependency: "direct main"
167181
description:
@@ -183,6 +197,20 @@ packages:
183197
url: "https://pub.dartlang.org"
184198
source: hosted
185199
version: "2.2.9"
200+
firebase_ml_model_downloader:
201+
dependency: "direct main"
202+
description:
203+
name: firebase_ml_model_downloader
204+
url: "https://pub.dartlang.org"
205+
source: hosted
206+
version: "0.1.0+9"
207+
firebase_ml_model_downloader_platform_interface:
208+
dependency: transitive
209+
description:
210+
name: firebase_ml_model_downloader_platform_interface
211+
url: "https://pub.dartlang.org"
212+
source: hosted
213+
version: "0.1.1+2"
186214
firebase_remote_config:
187215
dependency: "direct main"
188216
description:

packages/firebase_snippets_app/pubspec.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ dependencies:
2929
firebase_remote_config: ^2.0.2
3030
firebase_storage: ^10.2.9
3131
firebase_messaging: ^11.2.11
32-
path_provider: ^2.0.9
32+
firebase_dynamic_links: ^4.1.2
33+
firebase_ml_model_downloader: ^0.1.0+9
3334
firebase_auth: ^3.3.12
35+
path_provider: ^2.0.9
3436
google_sign_in: ^5.2.4
3537
flutter_facebook_auth: ^4.1.2
3638
github_sign_in: ^0.0.4

0 commit comments

Comments
 (0)