portal_core 1.0.1
portal_core: ^1.0.1 copied to clipboard
Annotation-driven model codegen (JSON, copyWith) plus a local cache and repository base for Portal apps.
Portal Core #
A Dart library for model generation with JSON serialization, caching, and repository patterns.
Project Structure #
This project has been simplified to use a single package structure instead of the previous split packages approach. The code generation logic is now directly integrated into the main package.
Key Files: #
lib/builder.dart- The build system entry pointlib/src/model_gen.dart- The code generation logiclib/annotations.dart- Annotation definitions for modelsbuild.yaml- Build system configuration
Usage #
1. Define your model with annotations: #
import 'package:portal_core/portal_core.dart';
@ModelGen(
cache: CacheConfig(key: 'users', collection: true),
repository: RepositoryConfig(endpoint: '/users'),
)
class User {
@Id
final String id;
final String name;
@Default(18)
final int age;
@Default([])
final List<String> tags;
final Set<int> scores;
@Default({'role': 'user'})
final Map<String, String> metadata;
User({
required this.id,
required this.name,
required this.age,
required this.tags,
required this.scores,
required this.metadata,
});
}
2. Run code generation: #
dart run build_runner build
3. Use the generated code: #
// The generated file will be user.portal.dart
import 'user.dart';
void main() async {
// Create a user with auto-generated ID
final user = UserFactory.auto(
name: 'John Doe',
scores: {85, 92, 78},
);
// JSON serialization
final json = user.toJson();
final fromJson = userFromJson(json);
// Use the cache repository
final cacheRepo = UserCacheRepository();
await cacheRepo.create(user);
final allUsers = await cacheRepo.getAll();
}
Features #
- JSON Serialization: Automatic
toJson()andfromJson()generation - Caching: Local storage using SharedPreferences
- Repository Pattern: Abstract repository interfaces and concrete implementations
- Auto ID Generation: UUID for strings, random integers for int IDs
- Default Values: Support for field defaults with
@Defaultannotation - Copy With: Automatic
copyWith()method generation - ID Field Detection: Automatic detection of ID fields or manual
@Idannotation
Annotations #
@ModelGen- Main annotation for model generation@Id- Marks a field as the primary identifier@Default(value)- Sets a default value for a field@JsonKey(name: 'custom_name')- Custom JSON key mapping
Build Configuration #
The build system is configured in build.yaml and pubspec.yaml to automatically generate .portal.dart files for all Dart files in lib/, example/, and test/ directories.
Why This Structure? #
The previous split-package approach was unnecessarily complex for a single project. This simplified structure:
- Eliminates circular dependencies between packages
- Reduces complexity in build configuration
- Makes the codebase easier to understand and maintain
- Simplifies dependency management
All code generation logic is now contained within the main package, making it self-contained and easier to work with.