Installing Ack
Add to Your Project
Add Ack to your project using the Dart CLI:
# For Dart projects
dart pub add ack
# For Flutter projects
flutter pub add ack
Or manually add to your pubspec.yaml (check pub.dev for the latest version):
dependencies:
ack: ^1.0.0 # Replace with latest version
Code Generator (@AckType())
If you want typed wrappers for hand-written schemas, add the annotation and
generator packages alongside ack:
dart pub add ack ack_annotations
dart pub add --dev ack_generator build_runner
Or add them to your pubspec.yaml:
dependencies:
ack: ^1.0.0 # Replace with latest version
ack_annotations: ^1.0.0 # Replace with latest version
dev_dependencies:
ack_generator: ^1.0.0 # Replace with latest version
build_runner: ^2.4.0
ack_generator does not generate schemas from classes. It reads top-level Ack
schema variables and getters annotated with @AckType() and emits typed
extension wrappers with parse() / safeParse() helpers.
import 'package:ack/ack.dart';
import 'package:ack_annotations/ack_annotations.dart';
part 'user.g.dart';
@AckType()
final userSchema = Ack.object({
'name': Ack.string(),
'email': Ack.string().email(),
});
Run the generator with:
dart run build_runner build --delete-conflicting-outputs
See the generator documentation for more AckType examples and supported schema shapes.
Basic Usage after Installation
Import Ack in your Dart files:
import 'package:ack/ack.dart';
// 1. Define a simple schema (e.g., for a name)
final nameSchema = Ack.string().minLength(3);
// 2. Validate some data against the schema
final result = nameSchema.safeParse('John');
// 3. Check the result
if (result.isOk) {
print('Valid: ${result.getOrThrow()}'); // Access the valid data
} else {
print('Invalid: ${result.getError()}'); // Get the error details
}
See the Quickstart Tutorial for a more complete example.