{"id":18895289,"url":"https://github.com/carllosnc/gty","last_synced_at":"2025-10-31T10:20:46.269Z","repository":{"id":243454207,"uuid":"812476353","full_name":"carllosnc/gty","owner":"carllosnc","description":"A easy way to perform GET http request in Flutter","archived":false,"fork":false,"pushed_at":"2024-07-31T19:45:36.000Z","size":96,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-31T07:43:40.609Z","etag":null,"topics":["flutter","http"],"latest_commit_sha":null,"homepage":"","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/carllosnc.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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":"2024-06-09T02:16:42.000Z","updated_at":"2024-07-31T19:45:41.000Z","dependencies_parsed_at":"2024-08-22T22:25:27.928Z","dependency_job_id":"719030a5-5073-4226-bb47-7182dcdf6522","html_url":"https://github.com/carllosnc/gty","commit_stats":null,"previous_names":["c4co/gty"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carllosnc%2Fgty","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carllosnc%2Fgty/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carllosnc%2Fgty/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carllosnc%2Fgty/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/carllosnc","download_url":"https://codeload.github.com/carllosnc/gty/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239869867,"owners_count":19710599,"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":["flutter","http"],"created_at":"2024-11-08T08:27:30.268Z","updated_at":"2025-10-31T10:20:46.262Z","avatar_url":"https://github.com/carllosnc.png","language":"Dart","readme":"# gty\n\n![Static Badge](https://img.shields.io/badge/Flutter-blue)\n[![GTY](https://github.com/carllosnc/gty/actions/workflows/dart.yml/badge.svg)](https://github.com/carllosnc/gty/actions/workflows/dart.yml)\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/b0420d42747f41fcbdb3f79b3c9cced2)](https://app.codacy.com/gh/carllosnc/gty/dashboard?utm_source=gh\u0026utm_medium=referral\u0026utm_content=\u0026utm_campaign=Badge_grade)\n\n\u003e A easy way to perform GET http request in Flutter\n\n## Installation\n\nAdd this to your package's pubspec.yaml file:\n\n```yaml\ngty:\n  git:\n    url: https://github.com/carllosnc/gty.git\n```\n\n## Using gty Mixin\n\nThe gty mixin is designed for Flutter's StatefulWidget to streamline HTTP GET requests and manage the loading, success, and error states. It provides a easy way to handle API interactions with built-in state management.\n\n**Features**:\n\n- [x] State Management — Automatically handles isLoading, isSuccess, and isError states during API requests.\n- [x] Response Handling — Parses JSON responses and provides easy access to transform the response data.\n- [x] Custom Headers — Allows for custom headers.\n- [x] Error Handling — Catches and manages errors gracefully.\n- [ ] Cachable — Support for memory cache\n\n**States**:\n\n- `isLoading`: Indicates that a request is in progress.\n- `isSuccess`: Indicates that the request completed successfully.\n- `isError`: Indicates that an error occurred during the request.\n- `response`: Holds the full HTTP response from the request.\n- `error`: Contains the error information if the request fails.\n- `data`: Provides access to the parsed JSON response data.\n- `viewData`: Provides a customized view of the response data, ex: adapting the data to a specific model.\n- `customLoading`: Allows for custom loading states.\n\n**fetchData({url, headers, onSuccess})**:\n\n- `url (required)`: The URL to which the HTTP GET request is sent.\n- `headers (optional)`: Additional headers to include in the request.\n- `onSuccess (optional)`: A callback function that is executed if the request is successful. This function receives the parsed JSON data as an argument and can be used to process the data.\n\n**A complete example**:\n\n```dart\nimport 'package:flutter/material.dart';\nimport 'package:gty/gty.dart';\nimport '../models/user.dart';\n\nclass ExampleOne extends StatefulWidget {\n  const ExampleOne({super.key});\n\n  @override\n  State\u003cExampleOne\u003e createState() =\u003e _ExampleOneState();\n}\n\nclass _ExampleOneState extends State\u003cExampleOne\u003e with gty {\n  @override\n  void initState() {\n    super.initState();\n\n    fetchData(\n      url: \"http://localhost:8080/users/1\",\n      onSuccess: (data) =\u003e adapt\u003cUser\u003e(data, User.fromJson),\n    );\n  }\n\n  @override\n  Widget build(BuildContext context) {\n    if (isLoading) {\n      return const Center(\n        child: Text(\"Loading...\"),\n      );\n    }\n\n    if (isError) {\n      return const Center(\n        child: Text(\"Error\"),\n      );\n    }\n\n    return Center(\n      child: Text((viewData as User).name),\n    );\n  }\n}\n```\n\n**Infer type of viewData**\n\nUsing the example above, the `viewData` can be inferred as a `User` object.\n\n```dart\nclass _ExampleOneState extends State\u003cExampleOne\u003e with gty\u003cExampleOne, User\u003e {\n  ...\n}\n```\n\n## adapt and adaptList\n\nThe `adapt` and `adaptList` functions are used to convert JSON data to a specific model class.\n\n**`adapt(dynamic data, Function(Map\u003cString, dynamic\u003e) T.fromJson) : T`**\n```dart\nadapt\u003cUser\u003e(data, User.fromJson)\n```\n\n**`adaptList(List\u003cdynamic\u003e data, Function(Map\u003cString, dynamic\u003e) T.fromJson) : List\u003cT\u003e`**\n```dart\nadaptList\u003cUser\u003e(data, User.fromJson)\n```\n\n## Using Gty Widget\n\nThe `Gty` widget is a custom `StatefulWidget` designed to simplify making HTTP GET requests and handling different states such as loading, success, and error. It utilizes the gty mixin to manage the request lifecycle and provide a flexible way to render UI based on the request's state.\n\n| Parameter    | Type                                                         | Description                                                                            |\n| ------------ | ------------------------------------------------------------ | -------------------------------------------------------------------------------------- |\n| `url`        | `String?`                                                    | The URL to send the HTTP GET request to.                                               |\n| `child`      | `Widget Function(BuildContext context, dynamic data)`        | A function that returns a widget, given the BuildContext and fetched data.             |\n| `error`      | `Widget Function(BuildContext context, GtyException error)?` | A function that returns a widget, given the BuildContext and an error.                 |\n| `loading`    | `Widget`                                                     | A widget to display while the data is being fetched. Defaults to `Text('Loading...')`. |\n| `onSuccess`  | `Function(dynamic)?`                                         | A callback function that is called when the request is successful.                     |\n| `httpClient` | `http.Client?`                                               | An optional custom HTTP client.                                                        |\n\n**Usage**:\n\n```dart\nimport 'package:flutter/material.dart';\nimport 'package:gty/gty.dart';\nimport '../models/user.dart';\n\nclass ExampleWidget extends StatelessWidget {\n  const ExampleWidget({super.key});\n\n  @override\n  Widget build(BuildContext context) {\n    return Gty(\n      url: \"http://localhost:8080/users/1\",\n      loading: const Text(\"Loading...\"),\n      error: (context, error) =\u003e Center(\n        child: Text(error.message),\n      ),\n      onSuccess: (data) =\u003e adapt\u003cUser\u003e(data, User.fromJson),\n      child: (context, data) {\n        return Text(\n          \"${(data as User).name} - From example widget\",\n        );\n      },\n    );\n  }\n}\n```\n\n## Development\n\n**Server**\n\nThe web server is provided by [dart_frog](https://pub.dev/packages/dart_frog).\n\n```bash\ncd server\ndart_frog dev\n```\n\n**Tests**\n\n```bash\nflutter test\n```\n\n---\n\nCarlos Costa @ 2024\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarllosnc%2Fgty","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcarllosnc%2Fgty","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarllosnc%2Fgty/lists"}