{"id":18048783,"url":"https://github.com/marcelgarus/hive_cache","last_synced_at":"2025-04-10T09:53:18.137Z","repository":{"id":56832424,"uuid":"215544985","full_name":"MarcelGarus/hive_cache","owner":"MarcelGarus","description":"A persistent cache that uses hive.","archived":false,"fork":false,"pushed_at":"2020-08-22T17:59:53.000Z","size":75,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T08:47:54.048Z","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":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MarcelGarus.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}},"created_at":"2019-10-16T12:39:39.000Z","updated_at":"2021-06-02T13:35:18.000Z","dependencies_parsed_at":"2022-09-08T05:10:48.629Z","dependency_job_id":null,"html_url":"https://github.com/MarcelGarus/hive_cache","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/MarcelGarus%2Fhive_cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarcelGarus%2Fhive_cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarcelGarus%2Fhive_cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarcelGarus%2Fhive_cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MarcelGarus","download_url":"https://codeload.github.com/MarcelGarus/hive_cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248198240,"owners_count":21063626,"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-10-30T20:15:23.462Z","updated_at":"2025-04-10T09:53:18.106Z","avatar_url":"https://github.com/MarcelGarus.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"While [\u003ckbd\u003ehive\u003c/kbd\u003e](https://pub.dev/packages/hive) allows you to save arbitrary objects to memory, you still need to worry about fetching data if necessary.\nUsually, when fetching data from a server, every item has a unique id.\nData items which have an `Id` are called  `Entity`s in this package.\nBoth `Id`s and `Entity`s are strongly typed:\n\n```dart\n@HiveType(typeId: 0)\nclass Fruit implements Entity\u003cFruit\u003e {\n  @HiveField(fieldId: 0)\n  final Id\u003cFruit\u003e id;\n  \n  @HiveField(fieldId: 1)\n  final String name;\n\n  @HiveField(fieldId: 2)\n  final int amount;\n}\n```\n\nBefore doing anything, you should initialize the `HiveCache`.\nInstead of registering your `TypeAdapter`s at `Hive` yourself, you can just register them at `HiveCache`, which does that for you.\nFor `Entity`'s, you should call `registerEntityType` instead of `registerAdapter` and provide a method that get executed whenever an `Entity` should be fetched:\n\n```dart\nawait HiveCache.initialize();\nHiveCache\n  ..registerAdapter(SomeAdapter())\n  ..registerEntityType(FruitAdapter(), (someId) =\u003e parse(await http.get('https://.../fruits/$someId')))\n  ..registerEntityType(SomeOtherEntityAdapter(), (id) =\u003e ...);\n```\n\nThen, if you have an `Id\u003cFruit\u003e`, you can simply use an `EntityBuilder` to build the `Fruit`:\n\n```dart\nfinal id = Id\u003cFruit\u003e('some-fruit');\n\n...\n\nEntityBuilder\u003cFruit\u003e(\n  id: Id\u003cFruit\u003e('some-fruit'),\n  builder: (context, snapshot, fetch) {\n    if (snapshot == null) {\n      // Still loading.\n      return Center(child: CircularProgressIndicator());\n    } else if (snapshot.hasData) {\n      // The snapshot contains data. It may be [null] if the fetch function\n      // returned [null].\n      return Text(snapshot.data);\n    } else if (snapshot.hasError) {\n      return Text('${snapshot.error}, ${snapshot.stackTrace}');\n    }\n    // Using [fetch], you can re-fetch data. By default, it only fetches from\n    // the cache. Use `fetch(force: true);` to fetch from the original source.\n  },\n),\n```\n\n## Live updating\n\nYou can call `saveToCache()` on any `Entity` to save it to the cache.\nAll `EntityBuilder`s that reference this `Entity` get automatically updated.\n\nYou can call `loadFromCache()` on any `Id\u003cT\u003e` to retrieve a `Stream\u003cT\u003e` of the entity.\nWhenever a new item gets saved to the cache, the `Stream` contains a new event with this item.\n\n## Lazy references\n\nYou can not only reference other `Entity`s by their `Id` or multiple `Entity`s by a `List\u003cId\u003e`, but you can also have lazy fetching of other entities:\n\n```dart\n@HiveType(typeId: 1)\nclass Person implements Entity\u003cPerson\u003e {\n  @HiveField(fieldId: 0)\n  final Id\u003cPerson\u003e id;\n  \n  @HiveField(fieldId: 1)\n  final String name;\n\n  // Lazy reference to an entity.\n  @HiveField(fieldId: 2)\n  final mom = Connection\u003cPerson\u003e(\n    id: 'mom of $id',\n    fetcher: () async =\u003e parse(await http.get('.../people?momOf=$id')),\n  );\n\n  // Lazy reference to multiple entities.\n  @HiveField(fieldId: 3)\n  final friends = Collection\u003cPerson\u003e(\n    id: 'friends of $id',\n    fetcher: () async =\u003e parse(await http.get('.../people?friendsWith=$id')),\n  );\n}\n```\n\nYou can use `ConnectionBuilder`s or `CollectionBuilder`s to build these `Entity`s similarly to how you would use an `EntityBuilder`.\nIn the builder, you get the `Id\u003cT\u003e` or `List\u003cId\u003cT\u003e\u003e` that the item references.\nIf you want to get the actual `Entity` or `List\u003cEntity\u003e`, you can use the `ConnectionBuilder.populated` and `CollectionBuilder.populated` constructors.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcelgarus%2Fhive_cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcelgarus%2Fhive_cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcelgarus%2Fhive_cache/lists"}