{"id":20664076,"url":"https://github.com/flutterando/dart-backend","last_synced_at":"2025-04-19T16:13:43.894Z","repository":{"id":135009812,"uuid":"342688207","full_name":"Flutterando/dart-backend","owner":"Flutterando","description":null,"archived":false,"fork":false,"pushed_at":"2023-03-22T00:20:38.000Z","size":3502,"stargazers_count":6,"open_issues_count":1,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-29T09:51:19.525Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Flutterando.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-26T20:05:55.000Z","updated_at":"2023-09-24T08:54:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"f50d33a7-45c0-4c00-b978-8964452ff6c3","html_url":"https://github.com/Flutterando/dart-backend","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flutterando%2Fdart-backend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flutterando%2Fdart-backend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flutterando%2Fdart-backend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flutterando%2Fdart-backend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Flutterando","download_url":"https://codeload.github.com/Flutterando/dart-backend/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249734933,"owners_count":21317934,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T19:22:02.636Z","updated_at":"2025-04-19T16:13:43.849Z","avatar_url":"https://github.com/Flutterando.png","language":"Dart","readme":"# JSON example\n\nThis example demonstrates writing a function that accepts and returns JSON.\n\nThe basic shape of the function handler looks like this:\n\n```dart\n@CloudFunction()\nGreetingResponse function(Map\u003cString, dynamic\u003e request) {\n}\n```\n\nThe client will send a JSON document using the HTTP POST method. Here's an\nexample request:\n\n```json\n{\n  \"name\": \"World\"\n}\n```\n\nThe function will send a JSON document as the response. Here's an example\nresponse after receiving the above request:\n\n```json\n{\n  \"salutation\": \"Hello\",\n  \"name\": \"World\"\n}\n```\n\nThe function handler actually receives the JSON request parsed as a map by the\nFunctions Framework. To access the expected `name` field of the request\ndocument (and provide a default value in case the `name` field is empty), the\ncode looks like this:\n\n```dart\n@CloudFunction()\nGreetingResponse function(Map\u003cString, dynamic\u003e request) {\n  final name = request['name'] as String ?? 'World';\n}\n```\n\nFinally, the function creates an object returns it. The Functions Framework will\ntake this and attempt \"do the right\" thing. In this case, the function is typed\nto return a `FutureOr\u003cGreetingResponse\u003e`, which has a `toJson()` method, so the\nframework will invoke this, then set the response body to the stringified result\nand set the response header (`content-type`) to `application/json`).\n\n```dart\n@CloudFunction()\nGreetingResponse function(Map\u003cString, dynamic\u003e request) {\n  final name = request['name'] as String ?? 'World';\n  final json = GreetingResponse(salutation: 'Hello', name: name);\n  return json;\n}\n```\n\nThe full code is shown below:\n\nlib/functions.dart\n\n```dart\nimport 'dart:async';\n\nimport 'package:functions_framework/functions_framework.dart';\nimport 'package:json_annotation/json_annotation.dart';\n\npart 'functions.g.dart';\n\n@JsonSerializable(nullable: false)\nclass GreetingResponse {\n  final String salutation;\n  final String name;\n\n  GreetingResponse({this.salutation, this.name});\n\n  Map\u003cString, dynamic\u003e toJson() =\u003e _$GreetingResponseToJson(this);\n}\n\n@CloudFunction()\nGreetingResponse function(Map\u003cString, dynamic\u003e request) {\n  final name = request['name'] as String ?? 'World';\n  final json = GreetingResponse(salutation: 'Hello', name: name);\n  return json;\n}\n```\n\n## Generate project files\n\nThe Dart `build_runner` tool generates the following files\n\n- `lib/functions.g.dart` - the part file for `GreetingResponse` serialization\n- `bin/server.dart` - the main entry point for the function server app, which\n  invokes the function\n\nRun the `build_runner` tool, as shown here:\n\n```shell\n$ dart run build_runner build\n[INFO] Generating build script completed, took 337ms\n[INFO] Reading cached asset graph completed, took 48ms\n[INFO] Checking for updates since last build completed, took 426ms\n[INFO] Running build completed, took 13ms\n[INFO] Caching finalized dependency graph completed, took 29ms\n[INFO] Succeeded after 51ms with 0 outputs (0 actions)\n```\n\n## Test the function\n\n```shell\n$ dart test\n00:02 +1: All tests passed!\n```\n\n## Run the function\n\n```shell\n$ dart run bin/server.dart\nListening on :8080\n```\n\nFrom another terminal, send a JSON request:\n\n```shell\n$ curl -X POST -H \"content-type: application/json\" -d '{ \"name\": \"World\" }' -i localhost:8080\nHTTP/1.1 200 OK\ndate: Sat, 19 Dec 2020 02:17:42 GMT\ncontent-length: 37\nx-frame-options: SAMEORIGIN\ncontent-type: application/json\nx-xss-protection: 1; mode=block\nx-content-type-options: nosniff\nserver: dart:io with Shelf\n\n{\"salutation\":\"Hello\",\"name\":\"World\"}\n```\n\nTools like [curl] (and [postman]) are good for sending HTTP requests. The\noptions used in this example are:\n\n- `-X POST` - send an HTTP POST request\n- `-H \"content-type: application/json\"` - set an HTTP header to indicate that\n  the body is a JSON document\n- `-d '{ \"name\": \"World\" }'` - set the request body to a JSON document\n- `-i` - show the response headers (to confirm the response body content type is\n  also a JSON document)\n\nThe last line, separated by a blank line, prints the response body.\n\nFor more details on getting started or to see how to run the function locally on\nDocker or deploy to Cloud Run, see these quick start guides:\n\n- [Quickstart: Dart]\n- [Quickstart: Docker]\n- [Quickstart: Cloud Run]\n\n\u003c!-- reference links --\u003e\n[curl]: https://curl.se/docs/manual.html\n[Quickstart: Dart]: https://github.com/GoogleCloudPlatform/functions-framework-dart/blob/main/docs/quickstarts/01-quickstart-dart.md\n[Quickstart: Docker]: https://github.com/GoogleCloudPlatform/functions-framework-dart/blob/main/docs/quickstarts/02-quickstart-docker.md\n[Quickstart: Cloud Run]: https://github.com/GoogleCloudPlatform/functions-framework-dart/blob/main/docs/quickstarts/03-quickstart-cloudrun.md\n[postman]: https://www.postman.com/product/api-client/\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflutterando%2Fdart-backend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflutterando%2Fdart-backend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflutterando%2Fdart-backend/lists"}