Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Dart/codelab-dart-functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.build/
.buildlog/
.history
.svn/
.swiftpm/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins-dependencies
.pub-cache/
.pub/
/build/
/coverage/

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
14 changes: 14 additions & 0 deletions Dart/codelab-dart-functions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Codelab Dart Functions

This project contains the source code for the "Deploy Dart on Firebase Functions" codelab.

See [the codelab](https://codelabs.developers.google.com/deploy-dart-on-firebase-functions) for full instructions.

## Running locally

This uses the Firebase emulator suite to test without deploying.

1. Ensure Firebase CLI is installed.
2. Initialize emulators in the root of the project: `firebase init emulators`
3. Start the emulators: `firebase emulators:start`
4. Run the flutter application `flutter run`.
28 changes: 28 additions & 0 deletions Dart/codelab-dart-functions/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at https://dart.dev/lints.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
19 changes: 19 additions & 0 deletions Dart/codelab-dart-functions/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"functions": [
{
"source": "functions",
"codebase": "dart-codelab-dart-functions",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log",
"*.local"
],
"predeploy": [
"dart pub get --directory=\"$RESOURCE_DIR\"",
"dart run build_runner build --delete-conflicting-outputs --directory=\"$RESOURCE_DIR\""
]
}
]
}
71 changes: 71 additions & 0 deletions Dart/codelab-dart-functions/functions/bin/server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import 'dart:convert';
import 'package:firebase_functions/firebase_functions.dart';
import 'package:google_cloud_firestore/google_cloud_firestore.dart'
show FieldValue;
import 'package:shared/shared.dart';

void main(List<String> args) async {
await fireUp(args, (firebase) {

// Listen for calls to the http request and name defined in the shared package.
firebase.https.onRequest(name: incrementCallable, (request) async {

// In a production app, verify the user with request.auth?.uid here.
// ignore: avoid_print
print('Incrementing counter on the server...');

// Get firestore database instance
final firestore = firebase.adminApp.firestore();

// Get a reference to the counter document
final counterDoc = firestore.collection('counters').doc('global');

// Get the current snapshot for the count data
final snapshot = await counterDoc.get();

// Increment response we will send back
IncrementResponse incrementResponse;

// Check for the current count and if the snapshot exists
if (snapshot.data() case {'count': int value} when snapshot.exists) {
if (request.method == 'GET') {
// Get the current result
incrementResponse = IncrementResponse(
success: true,
message: 'Read-only sync complete',
newCount: value,
);
} else if (request.method == 'POST') {
// Increment count by one
final step = request.url.queryParameters['step'] as int? ?? 1;
Comment thread
rodydavis marked this conversation as resolved.
await counterDoc.update({'count': FieldValue.increment(step)});
incrementResponse = IncrementResponse(
success: true,
message: 'Atomic increment complete',
newCount: value + step,
Comment thread
rodydavis marked this conversation as resolved.
);
} else {
throw FailedPreconditionError(
'only GET and POST requests are allowed',
);
Comment thread
rodydavis marked this conversation as resolved.
}
} else {
// Create a new document with a count of 1
await counterDoc.set({'count': 1});
incrementResponse = const IncrementResponse(
success: true,
message: 'Cloud-sync complete',
newCount: 1,
);
}

// Return the response as JSON
return Response(
200,
body: jsonEncode(incrementResponse.toJson()),
headers: {'Content-Type': 'application/json'},
);
});

});
}
5 changes: 5 additions & 0 deletions Dart/codelab-dart-functions/functions/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
targets:
$default:
sources:
- "bin/**"
- "lib/**"
Loading
Loading