https://github.com/venhdev/type_parser
type_parser
https://github.com/venhdev/type_parser
Last synced: 4 months ago
JSON representation
type_parser
- Host: GitHub
- URL: https://github.com/venhdev/type_parser
- Owner: venhdev
- License: mit
- Created: 2025-06-23T15:08:50.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-07-10T17:46:59.000Z (12 months ago)
- Last Synced: 2026-02-12T16:42:56.033Z (4 months ago)
- Language: Dart
- Size: 26.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# type_parser
A powerful and flexible Dart package for parsing and converting dynamic values to various types, including primitives, lists, and maps. It provides both safe (`tryParse`) and explicit (`parse`) methods for robust type handling.
## Features
- **Safe Parsing**: `tryParse` methods return `null` on failure, preventing runtime errors.
- **Explicit Parsing**: `parse` methods throw `FormatException` on failure, allowing for explicit error handling.
- **Extension Methods**: Convenient extension methods on `dynamic` for easy type conversion (e.g., `myDynamic.asString()`, `myDynamic.tryInt()`).
- **List and Map Parsing**: Robust handling for converting dynamic values to lists and maps, including JSON string parsing.
## Getting started
Add the following to your `pubspec.yaml`:
```yaml
dependencies:
type_parser: ^1.0.0
```
Then run `dart pub get`.
## Usage
### Basic Type Conversion
```dart
import 'package:type_parser/type_parser.dart';
void main() {
dynamic value = '123';
// Using asType methods (throws FormatException on failure)
int intValue = value.asInt(); // 123
print('asInt: $intValue');
dynamic anotherValue = 'true';
bool boolValue = anotherValue.asBool(); // true
print('asBool: $boolValue');
dynamic numValue = '3.14';
double doubleValue = numValue.asDouble(); // 3.14
print('asDouble: $doubleValue');
// Using tryType methods (returns null on failure)
dynamic invalidValue = 'abc';
int? tryIntValue = invalidValue.tryInt(); // null
print('tryInt: $tryIntValue');
dynamic validValue = '456';
int? tryValidIntValue = validValue.tryInt(); // 456
print('tryInt (valid): $tryValidIntValue');
}
```
### List and Map Conversion
```dart
import 'package:type_parser/type_parser.dart';
void main() {
dynamic jsonString = '[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]';
List> users = jsonString.asList(
itemConverter: (item) => item.asMap(),
); // [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
print('Users: $users');
dynamic csvString = 'apple,banana,orange';
List fruits = csvString.asList(); // ['apple', 'banana', 'orange']
print('Fruits: $fruits');
dynamic mapString = '{"key": "value", "number": 123}';
Map data = mapString.asMap(); // {'key': 'value', 'number': 123}
print('Data: $data');
}
```
## Additional information
Feel free to contribute, report issues, or suggest features on the [GitHub repository](https://github.com/venhdev/type_parser).