From 81b809958b8fc8a489e8d278022dda3193af384a Mon Sep 17 00:00:00 2001 From: Guillaume Bernos Date: Fri, 2 Aug 2024 09:08:00 +0200 Subject: [PATCH 1/2] chore: add the data bundle sample --- .../lib/snippets/firestore.dart | 23 +++++++++++++++++-- packages/firebase_snippets_app/pubspec.lock | 6 ++--- packages/firebase_snippets_app/pubspec.yaml | 1 + 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/firebase_snippets_app/lib/snippets/firestore.dart b/packages/firebase_snippets_app/lib/snippets/firestore.dart index 8929081..00ff776 100644 --- a/packages/firebase_snippets_app/lib/snippets/firestore.dart +++ b/packages/firebase_snippets_app/lib/snippets/firestore.dart @@ -15,11 +15,13 @@ // ignore_for_file: non_constant_identifier_names, avoid_print import 'dart:math'; +import 'dart:typed_data'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_snippets_app/model/firestore_add_data_custom_objects_snippet.dart'; import 'package:firebase_snippets_app/model/restaurant.dart'; import 'package:firebase_snippets_app/snippets/snippet_base.dart'; +import 'package:http/http.dart' as http; class FirestoreSnippets extends DocSnippet { @override @@ -102,9 +104,26 @@ class FirestoreSnippets extends DocSnippet { // [END data_model_sub_collections] } - void dataBundles_loadingClientBundles() { + void dataBundles_loadingClientBundles() async { // [START data_bundles_loading_client_bundles] - // TODO - currently in scratch file + // Get a bundle from a server + final url = Uri.https('example.com', '/create-bundle'); + final response = await http.get(url); + String string = response.body; + final buffer = Uint8List.fromList(string.codeUnits); + + // Load a bundle from a buffer + LoadBundleTask task = FirebaseFirestore.instance.loadBundle(buffer); + await task.stream.toList(); + + // Use the cached named query + final results = await FirebaseFirestore.instance.namedQueryGet( + "example-query", + options: const GetOptions( + source: Source.cache, + ), + ); + // [END data_bundles_loading_client_bundles] } diff --git a/packages/firebase_snippets_app/pubspec.lock b/packages/firebase_snippets_app/pubspec.lock index 27b8cc8..a458cd1 100644 --- a/packages/firebase_snippets_app/pubspec.lock +++ b/packages/firebase_snippets_app/pubspec.lock @@ -707,13 +707,13 @@ packages: source: hosted version: "2.3.0" http: - dependency: transitive + dependency: "direct main" description: name: http - sha256: "761a297c042deedc1ffbb156d6e2af13886bb305c2a343a4d972504cd67dd938" + sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010 url: "https://pub.dev" source: hosted - version: "1.2.1" + version: "1.2.2" http_multi_server: dependency: transitive description: diff --git a/packages/firebase_snippets_app/pubspec.yaml b/packages/firebase_snippets_app/pubspec.yaml index 835e1eb..fe64c91 100644 --- a/packages/firebase_snippets_app/pubspec.yaml +++ b/packages/firebase_snippets_app/pubspec.yaml @@ -45,6 +45,7 @@ dependencies: crypto: ^3.0.1 firebase_crashlytics: ^3.3.0 firebase_auth_platform_interface: ^7.3.0 + http: ^1.2.2 dev_dependencies: flutter_test: From 44929b0edfb3d43947b078f3c8dda3557cceeb6a Mon Sep 17 00:00:00 2001 From: Guillaume Bernos Date: Thu, 3 Oct 2024 08:50:49 +0200 Subject: [PATCH 2/2] apply suggestions --- packages/firebase_snippets_app/lib/snippets/firestore.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/firebase_snippets_app/lib/snippets/firestore.dart b/packages/firebase_snippets_app/lib/snippets/firestore.dart index 00ff776..6873a82 100644 --- a/packages/firebase_snippets_app/lib/snippets/firestore.dart +++ b/packages/firebase_snippets_app/lib/snippets/firestore.dart @@ -109,8 +109,8 @@ class FirestoreSnippets extends DocSnippet { // Get a bundle from a server final url = Uri.https('example.com', '/create-bundle'); final response = await http.get(url); - String string = response.body; - final buffer = Uint8List.fromList(string.codeUnits); + String body = response.body; + final buffer = Uint8List.fromList(body.codeUnits); // Load a bundle from a buffer LoadBundleTask task = FirebaseFirestore.instance.loadBundle(buffer); @@ -118,7 +118,7 @@ class FirestoreSnippets extends DocSnippet { // Use the cached named query final results = await FirebaseFirestore.instance.namedQueryGet( - "example-query", + "latest-stories-query", options: const GetOptions( source: Source.cache, ),