type_caster 0.0.3
type_caster: ^0.0.3 copied to clipboard
A powerful type casting and conversion library for Dart that provides safe and flexible type conversion utilities.
Type Caster #
A powerful type casting and conversion library for Dart that provides safe and flexible type conversion utilities.
Features #
- Safe type casting with clear error handling
- Support for common Dart types (int, double, String, bool, List, Set, etc.)
- Extensible extension API system for type conversion (e.g., StringApi)
- Advanced string utilities, including flexible truncation
- ListCaster supports Set to List conversion and robust string parsing
- Null safety support
- Clear and descriptive error messages
- No external dependencies
Installation #
Add type_caster
to your pubspec.yaml
:
dependencies:
type_caster: ^0.0.3
Then run:
flutter pub get
# or
# dart pub get
Usage #
Basic Usage #
import 'package:type_caster/type_caster.dart';
void main() {
final Object? value = '123';
// Global Functions
final int? intValue = tryAs<int>(value);
print('tryAs<int>(value): $intValue'); // Output: 123
final String stringValue = asString(value);
print('asString(value): $stringValue'); // Output: 123
final bool boolValue = asBool(value, orElse: () => false);
print('asBool(value, orElse: () => false): $boolValue'); // Output: false
final List<int> listValue = asList<int>('[1,2,3]', itemDecoder: (e) => asInt(e));
print('asList<int>(\'[1,2,3]\', itemDecoder: (e) => asInt(e)): $listValue'); // Output: [1, 2, 3]
// Handle errors
try {
final bool boolValue = asBool(value);
} on CastException catch (e) {
print('Failed to cast: ${e.message}');
}
}
String Truncation #
The new extension API provides advanced string truncation:
import 'package:type_caster/type_caster.dart';
void main() {
final text = 'Hello, this is a long string!';
print(text.truncate(10)); // 'Hello, ...'
print(text.truncate(10, omission: '***', position: TruncatePosition.start)); // '***ng string!'
}
TruncatePosition.end
(default): Truncates at the end, appending the omission.TruncatePosition.start
: Truncates at the start, prepending the omission.
List and Set Conversion #
import 'package:type_caster/type_caster.dart';
void main() {
final set = {'1', '2', '3'};
final list = set.asList<String>();
print(list); // [1, 2, 3]
final csv = 'a,b,c';
final listFromString = csv.asList<String>();
print(listFromString); // [a, b, c]
}
Custom Caster Example #
class User {
final String name;
final int age;
User({required this.name, required this.age});
factory User.fromJson(Map<String, dynamic> json) {
return User(
name: json['name']?.asString(),
age: json['age']?.asInt(),
);
}
}
API Reference #
Extension Methods (via Extension API) #
String.truncate([int? maxLength, String omission = '...', TruncatePosition position = TruncatePosition.end])
: Truncate a string with flexible options.String asString([String Function()? orElse])
: Casts the string toString
or throwsCastException
.String? tryString([String Function()? orElse])
: Tries to cast the string toString
.num asNum([num Function()? orElse])
: Casts the string tonum
or throwsCastException
.num? tryNum([num Function()? orElse])
: Tries to cast the string tonum
.int asInt([int Function()? orElse])
: Casts the string toint
or throwsCastException
.int? tryInt([int Function()? orElse])
: Tries to cast the string toint
.double asDouble([double Function()? orElse])
: Casts the string todouble
or throwsCastException
.double? tryDouble([double Function()? orElse])
: Tries to cast the string todouble
.bool asBool([bool Function()? orElse])
: Casts the string tobool
or throwsCastException
.bool? tryBool([bool Function()? orElse])
: Tries to cast the string tobool
.List<T> asList<T>([List<T> Function()? orElse, T Function(dynamic)? itemDecoder, String separator = ','])
: Casts the string toList<T>
or throwsCastException
.List<T>? tryList<T>([List<T> Function()? orElse, T Function(dynamic)? itemDecoder, String separator = ','])
: Tries to cast the string toList<T>
.
Enum
enum TruncatePosition { start, end }
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Support #
If you find this package useful, please consider giving it a star on GitHub.
For issues and feature requests, please use the issue tracker.