{"id":20616740,"url":"https://github.com/andyduke/dynamic_value","last_synced_at":"2025-06-25T23:04:15.516Z","repository":{"id":56828277,"uuid":"339720579","full_name":"andyduke/dynamic_value","owner":"andyduke","description":"The easy way to work with JSON data.","archived":false,"fork":false,"pushed_at":"2024-04-08T20:49:55.000Z","size":34,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-25T23:03:43.694Z","etag":null,"topics":["api","conversion","data","dynamic","flutter","json"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/dynamic_value","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andyduke.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,"zenodo":null}},"created_at":"2021-02-17T12:43:18.000Z","updated_at":"2025-02-23T23:07:08.000Z","dependencies_parsed_at":"2025-04-15T07:55:52.790Z","dependency_job_id":"66017873-b0c0-4040-9f45-ea7a7651159b","html_url":"https://github.com/andyduke/dynamic_value","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/andyduke/dynamic_value","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyduke%2Fdynamic_value","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyduke%2Fdynamic_value/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyduke%2Fdynamic_value/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyduke%2Fdynamic_value/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andyduke","download_url":"https://codeload.github.com/andyduke/dynamic_value/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyduke%2Fdynamic_value/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261967126,"owners_count":23237662,"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":["api","conversion","data","dynamic","flutter","json"],"created_at":"2024-11-16T11:20:07.465Z","updated_at":"2025-06-25T23:04:15.478Z","avatar_url":"https://github.com/andyduke.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DynamicValue\r\n\r\nThe easy way to work with JSON data.\r\n\r\n## Getting Started\r\n\r\nJSON response like this:\r\n```json\r\nfinal json = {\r\n    \"id\": 1,\r\n    \"name\": \"User 1\",\r\n    \"created\": \"2021-02-17 00:00:00\",\r\n    \"groups\": [\r\n        {\r\n            \"id\": \"1\",\r\n            \"name\": \"Group 1\",\r\n        },\r\n    ],\r\n}\r\n```\r\n\r\nYou can access its fields as follows:\r\n```dart\r\nfinal value = DynamicValue(json);\r\n\r\nvalue['id'].toInt                   // -\u003e 1\r\nvalue['name'].toStr                 // -\u003e \"User 1\"\r\nvalue['created'].toDateTime         // -\u003e DateTime('2021-02-17 00:00:00.000')\r\nvalue['groups'][0]['id'].toInt      // -\u003e 1\r\nvalue['groups'][0].to\u003cGroup\u003e()      // -\u003e Group(id: 1, name: \"Group 1\")\r\nvalue['groups'].toList\u003cGroup\u003e()     // -\u003e [Group(id: 1, name: \"Group 1\")]\r\n```\r\n\r\nYou don't have to worry about whether there is a key in the object or an index in the list, what type of value is passed (for example, numbers can be represented in JSON both as numbers and as a string), you don't need to convert the date and time to the corresponding object or a nested structure in a model class. DynamicValue takes care of all this.\r\n\r\n## Data access\r\n\r\nYou can use text keys for objects and numeric indexes for lists to access structure fields.\r\n\r\n```dart\r\nvalue['id']     // Key access\r\nvalue[0]        // Access by index\r\n```\r\n\r\nIf there is no key or index, or the value is not an object or a list, `DynamicValue` object will be returned with a `null` value.\r\nOtherwise a `DynamicValue` object will be returned with a value that can be converted to the desired type. \r\n\r\n```dart\r\nvalue['not_existing_key'].toInt                         // null\r\nvalue[4343].toDouble                                    // null\r\nvalue['not_existing_key'].toInt ?? 1                    // 1\r\nvalue['not_existing_key'].to\u003cint\u003e(defaultValue: 1)      // 1\r\n```\r\n\r\nThis allows you to safely access nested data, if there is no key or index, then the result will be `DynamicValue(null)`, which can be converted to the desired type with a default value or null.\r\n\r\n\r\n## Data conversion\r\n\r\nDynamicValue can be converted to various data types using the `.to\u003cT\u003e()` method:\r\n```dart\r\nvalue['id'].to\u003cint\u003e()\r\n```\r\n\r\nThe following types are supported by default:\r\n- num\r\n- int\r\n- double\r\n- bool\r\n- String\r\n- DateTime\r\n\r\nThere are helper getters for them:\r\n- toNum\r\n- toInt\r\n- toDouble\r\n- toBool\r\n- toStr\r\n- toDateTime\r\n\r\nType conversion can be extended by specifying the type and converter function in the `builders` and `rawBuilders` static properties:\r\n```dart\r\nDynamicValue.builders[User] = User.fromDynamicValue;\r\nDynamicValue.rawBuilders[Group] = Group.fromMap;\r\n```\r\n\r\nYou can also specify a converter function as a parameter of the `.to\u003cT\u003e()` method:\r\n```dart\r\ndata.to\u003cUser\u003e(builder: User.fromDynamicValue)           // User(id: 1, ...)\r\ndata['groups'][0].to\u003cGroup\u003e(rawBuilder: Group.fromMap)  // Group(id: 1, ...)\r\n```\r\n\r\n\u003e **For builder**: the converter function must accept `DynamicValue` as input and return the required type.\r\n\u003e \r\n\u003e **For rawBuilder**: the converter function must accept a dart data type as input (for example Map, List, int, String, etc.) and return the required type. \r\n\r\n\r\nYou can specify a default value using the `defaultValue` parameter, if the value cannot be converted:\r\n```dart\r\n.to\u003cint\u003e(defaultValue: 4)\r\n```\r\n\r\nYou can convert a list of values to a list of the specified type using the `.toList\u003cT\u003e()` method:\r\n```dart\r\nvalue['groups'].toList\u003cGroup\u003e()          // \u003cGroup\u003e[Group(id: 1, ...)]\r\n```\r\n\r\nYou can convert a map of values to a map of the specified type using the `.toMap\u003cK, V\u003e()` method:\r\n```json\r\nfinal json = {\r\n    \"map-groups\": {\r\n        \"test-group-1\": {\r\n            \"id\": \"1\",\r\n            \"name\": \"Group 1\",\r\n        }\r\n    }\r\n}\r\n```\r\n```dart\r\nfinal value = DynamicValue(json);\r\nvalue['map-groups'].toMap\u003cString, Group\u003e()          // \u003cString, Group\u003e{'test-group-1': Group(id: 1, ...)}\r\n```\r\n\r\n\r\n## Helpers\r\n\r\nYou can check if an object has a specific key or a specific index in the list using the `.has()` method:\r\n```dart\r\nvalue.has('id')                  // true\r\nvalue['groups'].has(2)           // false\r\n```\r\n\r\nYou can also check if `DynamicValue` contains `null` using the following two properties:\r\n```dart\r\nvalue.isNull             // Returns true if value is null\r\nvalue.isNotNull          // Returns true if value is NOT null\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandyduke%2Fdynamic_value","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandyduke%2Fdynamic_value","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandyduke%2Fdynamic_value/lists"}