-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Dart #1255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jhuleatt
wants to merge
2
commits into
main
Choose a base branch
from
dart-launch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Dart #1255
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| name: CI Tests | ||
|
|
||
| on: | ||
| pull_request: | ||
| paths: | ||
| - 'Dart/**' | ||
|
|
||
| push: | ||
| branches: | ||
| - main | ||
| paths: | ||
| - 'Dart/**' | ||
|
|
||
| env: | ||
| CI: true | ||
|
|
||
| jobs: | ||
| unit: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: | ||
| dart-version: | ||
| - "3.11.4" | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Dart ${{ matrix.dart-version }} | ||
| uses: dart-lang/setup-dart@v1.6.0 | ||
| with: | ||
| sdk: ${{ matrix.dart-version }} | ||
|
|
||
| - name: Test Dart Samples | ||
| run: | | ||
| set -e | ||
| for dir in $(find Dart -name pubspec.yaml -exec dirname {} \;); do | ||
| echo "::group::Testing $dir" | ||
| cd "$dir" | ||
| dart pub get | ||
| dart format --set-exit-if-changed . | ||
| dart analyze . | ||
| cd - > /dev/null | ||
| echo "::endgroup::" | ||
| done | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| .dart_tool/ | ||
| .packages | ||
| build/ | ||
| *.dart_tool | ||
| pubspec.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Firebase SDK for Cloud Functions Quickstart - Callable Functions Streaming | ||
|
|
||
| This quickstart demonstrates how to send requests to a server-side function and _stream_ a response to a client SDK using Dart. | ||
|
|
||
| [Read more about Cloud Functions for Firebase](https://firebase.google.com/docs/functions/) | ||
|
|
||
|
|
||
| ## Frontend Client | ||
|
|
||
| You can find the frontend code in [`../../../Node/quickstarts/callable-functions-streaming/website`](../../../Node/quickstarts/callable-functions-streaming/website). | ||
|
|
||
| **IMPORTANT NOTE**: Due to limitations during the experimental release of Dart support in Cloud Functions for Firebase, the code in the website must be modified. Instead of using `httpsCallable`, you must use [`httpsCallableFromURL`](https://firebase.google.com/docs/reference/js/functions.md#httpscallablefromurl_7af6987) and pass the full function url (`https://<function-name-and-hash>.run.app` or the emulator equivalent), which can be found in the Cloud console or the emulator logs. | ||
|
|
||
|
|
||
| ## Deploy the app to prod | ||
|
|
||
| First you need to get the `dart` dependencies of the functions: | ||
|
|
||
| ```bash | ||
| dart pub get | ||
| ``` | ||
|
|
||
| Deploy to Firebase using the following command: | ||
|
|
||
| ```bash | ||
| firebase deploy --only functions | ||
| ``` | ||
|
|
||
| ## License | ||
|
|
||
| © Google, 2026. Licensed under an [Apache-2](../../../LICENSE) license. |
92 changes: 92 additions & 0 deletions
92
Dart/quickstarts/callable-functions-streaming/bin/server.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // [START full-sample] | ||
| // Dependencies for callable functions. | ||
| import 'dart:convert'; | ||
| import 'dart:math'; | ||
|
|
||
| import 'package:firebase_functions/firebase_functions.dart'; | ||
| import 'package:http/http.dart' as http; | ||
|
|
||
| /// Gets the weather from the national weather service | ||
| /// https://www.weather.gov/documentation/services-web-api | ||
| Future<dynamic> weatherForecastApi(num lat, num lng) async { | ||
| final resp = await http.get( | ||
| Uri.parse('https://api.weather.gov/points/$lat,$lng'), | ||
| ); | ||
|
|
||
| if (resp.statusCode >= 400) { | ||
| return 'error: ${resp.statusCode}'; | ||
| } | ||
|
|
||
| final pointData = jsonDecode(resp.body) as Map<String, dynamic>; | ||
| final forecastUrl = pointData['properties']['forecast'] as String; | ||
| final forecastResp = await http.get(Uri.parse(forecastUrl)); | ||
|
|
||
| if (forecastResp.statusCode >= 400) { | ||
| return 'error: ${forecastResp.statusCode}'; | ||
| } | ||
|
|
||
| // add an artificial wait to emphasize stream-iness | ||
| final randomWait = Random().nextDouble() * 1500; | ||
| await Future<void>.delayed(Duration(milliseconds: randomWait.toInt())); | ||
|
|
||
| return jsonDecode(forecastResp.body); | ||
| } | ||
|
|
||
| void main(List<String> args) async { | ||
| await fireUp(args, (firebase) { | ||
| // [START streaming-callable] | ||
| firebase.https.onCall(name: 'getForecast', (request, response) async { | ||
| final data = request.data as Map<String, Object?>?; | ||
| final locations = data?['locations'] as List<dynamic>?; | ||
|
|
||
| if (locations == null || locations.isEmpty) { | ||
| throw InvalidArgumentError('Missing locations to forecast'); | ||
| } | ||
|
|
||
| // fetch forecast data for all requested locations | ||
| final allRequests = locations.map((location) async { | ||
| if (location case { | ||
| 'latitude': num latitude, | ||
| 'longitude': num longitude, | ||
| }) { | ||
| final forecast = await weatherForecastApi(latitude, longitude); | ||
| final result = { | ||
| 'latitude': latitude, | ||
| 'longitude': longitude, | ||
| 'forecast': forecast, | ||
| }; | ||
|
|
||
| // clients that support streaming will have each | ||
| // forecast streamed to them as they complete | ||
| if (request.acceptsStreaming) { | ||
| await response.sendChunk(result); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| throw InvalidArgumentError('Invalid location format'); | ||
| }); | ||
|
|
||
| // Return the full set of data to all clients | ||
| return CallableResult(await Future.wait(allRequests)); | ||
| }); | ||
| // [END streaming-callable] | ||
| }); | ||
| } | ||
|
|
||
| // [END full-sample] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "functions": { | ||
| "source": ".", | ||
| "codebase": "dart-quickstarts-callable-functions-streaming" | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
Dart/quickstarts/callable-functions-streaming/pubspec.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| name: callable_functions_streaming | ||
| description: Streaming callable examples for Firebase Functions for Dart | ||
| publish_to: none | ||
|
|
||
| environment: | ||
| sdk: ^3.11.0 | ||
|
|
||
| dependencies: | ||
| firebase_functions: | ||
| git: | ||
| url: https://github.com/firebase/firebase-functions-dart | ||
| ref: main | ||
| http: ^1.2.0 | ||
|
|
||
| dev_dependencies: | ||
| build_runner: ^2.10.5 | ||
| lints: ^6.0.0 | ||
|
|
||
| dependency_overrides: | ||
| firebase_functions: | ||
| git: | ||
| url: https://github.com/firebase/firebase-functions-dart | ||
| ref: main | ||
| dart_firebase_admin: | ||
| git: | ||
| url: https://github.com/firebase/firebase-admin-dart | ||
| path: packages/dart_firebase_admin | ||
| ref: main | ||
| google_cloud_firestore: | ||
| git: | ||
| url: https://github.com/firebase/firebase-admin-dart | ||
| path: packages/google_cloud_firestore | ||
| ref: main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| .dart_tool/ | ||
| .packages | ||
| build/ | ||
| *.dart_tool | ||
| pubspec.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Firebase SDK for Cloud Functions Quickstart - Callable Functions | ||
|
|
||
| This quickstart demonstrates how to send requests to a server-side function and get a response back using one of the Client SDKs. | ||
|
|
||
| ## Introduction | ||
|
|
||
| The function `addNumbers` Returns the sum of two numbers. | ||
|
|
||
| Further reading: | ||
|
|
||
| - [Read more about Cloud Functions for Firebase](https://firebase.google.com/docs/functions/) | ||
|
|
||
| ## Deploy the app to prod | ||
|
|
||
| First you need to get the `dart` dependencies of the functions: | ||
|
|
||
| ```bash | ||
| dart pub get | ||
| ``` | ||
|
|
||
| Deploy to Firebase using the following command: | ||
|
|
||
| ```bash | ||
| firebase deploy --only functions | ||
| ``` | ||
|
|
||
| ## License | ||
|
|
||
| © Google, 2026. Licensed under an [Apache-2](../../../LICENSE) license. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // [START imports] | ||
| // Dependencies for callable functions. | ||
| import 'package:firebase_functions/firebase_functions.dart'; | ||
| // [END imports] | ||
|
|
||
| void main(List<String> args) async { | ||
| await fireUp(args, (firebase) { | ||
| // [START allAdd] | ||
| // [START addFunctionTrigger] | ||
| // Adds two numbers to each other. | ||
| firebase.https.onCall(name: 'addNumbers', (request, response) async { | ||
| // [END addFunctionTrigger] | ||
| // [START readAddData] | ||
| // Numbers passed from the client. | ||
| final data = request.data as Map<String, Object?>?; | ||
| final firstNumber = data?['firstNumber']; | ||
| final secondNumber = data?['secondNumber']; | ||
| // [END readAddData] | ||
|
|
||
| // [START addHttpsError] | ||
| // Checking that attributes are present and are numbers. | ||
| if (firstNumber is! num || secondNumber is! num) { | ||
| // Throwing an HttpsError so that the client gets the error details. | ||
| throw InvalidArgumentError( | ||
| 'The function must be called with two arguments "firstNumber" and "secondNumber" which must both be numbers.', | ||
| ); | ||
| } | ||
| // [END addHttpsError] | ||
|
|
||
| // [START authIntegration] | ||
| // Authentication / user information is automatically added to the request. | ||
| final uid = request.auth?.uid; | ||
| final token = request.auth?.token; | ||
| final name = token?['name']; | ||
| final picture = token?['picture']; | ||
| final email = token?['email']; | ||
|
|
||
| // Use variables to suppress 'unused' lint warnings | ||
| print( | ||
| 'User details: uid=$uid, name=$name, picture=$picture, email=$email', | ||
| ); | ||
| // [END authIntegration] | ||
|
|
||
| // [START returnAddData] | ||
| // returning result. | ||
| return CallableResult({ | ||
| 'firstNumber': firstNumber, | ||
| 'secondNumber': secondNumber, | ||
| 'operator': '+', | ||
| 'operationResult': firstNumber + secondNumber, | ||
| }); | ||
| // [END returnAddData] | ||
| }); | ||
| // [END allAdd] | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "functions": { | ||
| "source": ".", | ||
| "codebase": "dart-quickstarts-callable-functions" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| name: callable_functions | ||
| description: Callable examples for Firebase Functions for Dart | ||
| publish_to: none | ||
|
|
||
| environment: | ||
| sdk: ^3.11.0 | ||
|
|
||
| dependencies: | ||
| firebase_functions: | ||
| git: | ||
| url: https://github.com/firebase/firebase-functions-dart | ||
| ref: main | ||
|
|
||
| dev_dependencies: | ||
| build_runner: ^2.10.5 | ||
| lints: ^6.0.0 | ||
|
|
||
| dependency_overrides: | ||
| firebase_functions: | ||
| git: | ||
| url: https://github.com/firebase/firebase-functions-dart | ||
| ref: main | ||
| dart_firebase_admin: | ||
| git: | ||
| url: https://github.com/firebase/firebase-admin-dart | ||
| path: packages/dart_firebase_admin | ||
| ref: main | ||
| google_cloud_firestore: | ||
| git: | ||
| url: https://github.com/firebase/firebase-admin-dart | ||
| path: packages/google_cloud_firestore | ||
| ref: main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| .dart_tool/ | ||
| .packages | ||
| build/ | ||
| *.dart_tool | ||
| pubspec.lock |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Workflow does not contain permissions Medium test