{"id":19326886,"url":"https://github.com/justprodev/json_fetcher","last_synced_at":"2025-04-22T20:33:00.531Z","repository":{"id":61258381,"uuid":"336588183","full_name":"justprodev/json_fetcher","owner":"justprodev","description":"Fetching JSON data with caching/auth-handling/logging","archived":false,"fork":false,"pushed_at":"2024-11-06T21:21:37.000Z","size":556,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-06T21:28:23.731Z","etag":null,"topics":["cache","dart","flutter","http","json"],"latest_commit_sha":null,"homepage":"","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/justprodev.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2021-02-06T16:55:51.000Z","updated_at":"2024-09-01T13:25:06.000Z","dependencies_parsed_at":"2024-03-04T19:58:31.907Z","dependency_job_id":"a9001dc3-859a-4b83-a4ed-7d791a13b2d6","html_url":"https://github.com/justprodev/json_fetcher","commit_stats":null,"previous_names":[],"tags_count":67,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justprodev%2Fjson_fetcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justprodev%2Fjson_fetcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justprodev%2Fjson_fetcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justprodev%2Fjson_fetcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/justprodev","download_url":"https://codeload.github.com/justprodev/json_fetcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223903366,"owners_count":17222552,"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":["cache","dart","flutter","http","json"],"created_at":"2024-11-10T02:15:13.447Z","updated_at":"2025-04-22T20:33:00.525Z","avatar_url":"https://github.com/justprodev.png","language":"Dart","readme":"[![pub package](https://img.shields.io/pub/v/json_fetcher.svg)](https://pub.dev/packages/json_fetcher)\n[![codecov](https://codecov.io/gh/justprodev/json_fetcher/graph/badge.svg?token=2EOK5RXNB4)](https://codecov.io/gh/justprodev/json_fetcher)\n\n## Motivation\n\nImagine a user launching the app for the second time and seeing a skeletal loading screen or progress bar,\nespecially in parts of the UI where the data doesn't change often. This is not a good UX.\n\nTo fix this, we can load the data from the cache in the first step, and then update the data in the second step.\n\n*We don't aim to minimize requests to the server, but to minimize the time it takes to display the data to the user.*\n\n## Using\n\n```dart\nimport 'package:http/http.dart' as http;\nimport 'package:json_fetcher/json_fetcher.dart';\nimport 'package:path_provider/path_provider.dart';\n\nFuture\u003cString\u003e get cachePath =\u003e getApplicationCacheDirectory().then((dir) =\u003e dir.path);\nfinal client = JsonHttpClient(http.Client(), createCache(cachePath));\nfinal postsStream = JsonFetcher\u003cModel\u003e\u003e(\n  client,\n  (json) =\u003e Model.fromJson(json),\n).fetch('https://example.com/get-json');\n```\n\n\u003e [!TIP]\n\u003e Examples can be found in the [example](https://github.com/justprodev/json_fetcher/tree/master/example) directory:\n\u003e - [Flutter example](https://github.com/justprodev/json_fetcher/tree/master/example/flutter_json_fetcher_example)\n\u003e - [Pure Dart example](https://github.com/justprodev/json_fetcher/tree/master/example/dart_json_fetcher_example)\n\u003e - [Flutter example deployed to the web](https://justprodev.com/demo/json_fetcher_flutter/)\n\n\n## Configuration\n\nAt the first step you should create [JsonHttpClient](https://github.com/justprodev/json_fetcher/blob/master/lib/src/json_http_client.dart):\n\n```dart\nfinal jsonClient = JsonHttpClient(httpClient, cache);\n```\n\n### HttpClient\n\nThis package uses standard Dart [http](https://pub.dev/packages/http) package.\n\nBasicaly, is enough to use [Client()](https://pub.dev/documentation/http/latest/http/Client-class.html) for creating raw client:\n\n```dart\nfinal jsonClient = JsonHttpClient(Client(), cache);\n```\n\nSo, you can [configure client](https://pub.dev/packages/http#2-configure-the-http-client) more precisely before creating [JsonHttpClient](https://github.com/justprodev/json_fetcher/blob/master/lib/src/json_http_client.dart) itself.\n\n```dart\nClient httpClient() {\n  if (Platform.isAndroid) {\n    final engine = CronetEngine.build(\n        cacheMode: CacheMode.memory,\n        cacheMaxSize: 1000000);\n    return CronetClient.fromCronetEngine(engine);\n  }\n  if (Platform.isIOS || Platform.isMacOS) {\n    final config = URLSessionConfiguration.ephemeralSessionConfiguration()\n      ..cache = URLCache.withCapacity(memoryCapacity: 1000000);\n    return CupertinoClient.fromSessionConfiguration(config);\n  }\n  return IOClient();\n}\n\nfinal jsonClient = JsonHttpClient(httpClient(), cache);\n```\n\n\u003e [!TIP]\n\u003e The package contains convenitent client-wrapper with logging capabitility [LoggableHttpClient](https://github.com/justprodev/json_fetcher/blob/master/lib/loggable_http_client.dart),\n\u003e see [Flutter example](https://github.com/justprodev/json_fetcher/tree/master/example/flutter_json_fetcher_example) as use case.\n\n\n### Cache\n\nFor creating `cache` just use convenient function `createCache(path)`.\n\n\u003e [!NOTE]\n\u003e `path` can be `String` of `Future\u003cString\u003e`\n\u003e\n\u003e `Future\u003cString\u003e` variant is very useful for creating a client somewhere in `DI` at app startup, to prevent unneded waiting.\n\nIn Flutter Android/iOS app you can create client that caches data in standard cache directory with following snippet:\n\n```dart\nimport 'package:path_provider/path_provider.dart';\n\nFuture\u003cString\u003e get path =\u003e getApplicationCacheDirectory().then((value) =\u003e value.path);\n\nfinal jsonClient = JsonHttpClient(httpClient(), createCache(path));\n```\n\n\u003e [!TIP]\n\u003e Package exposes interface [HttpCache](https://github.com/justprodev/json_fetcher/blob/master/lib/src/http_cache.dart), that can be implemented to use your own cache.\n\n## How it works\n\nIf the data has changed (new data has arrived from the network), it will be updated in the second step.\nThat's why we use ```Stream\u003cT\u003e``` instead of ```Future\u003cT\u003e```. The stream's subscriber will get two snapshots.\n\nIf the data hasn't changed, the stream's subscriber will get one snapshot\n\nIf there is no cached copy, the stream's subscriber will get one snapshot.\n\n### Cache\n\nThe cache data is managed by implementations of [HttpCache](https://github.com/justprodev/json_fetcher/tree/master/lib/src/http_cache.dart).\n\n#### dart:io (mobile/desktop)\n\n[HttpFilesCache](https://github.com/justprodev/json_fetcher/tree/master/lib/src/cache/http_files_cache/http_files_cache.dart) stores data in files.\nPerformance improved by using concurrent IO with synchronization by keys.\n\n#### Web\n\n[HttpWebCache](https://github.com/justprodev/json_fetcher/tree/master/lib/src/cache/http_web_cache/http_web_cache.dart) stores data in [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API).\nIt uses [package:web](https://pub.dev/packages/web) from Dart team.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustprodev%2Fjson_fetcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjustprodev%2Fjson_fetcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustprodev%2Fjson_fetcher/lists"}