HasSlug mixin
Mixin that adds slug generation support to models
Automatically generates URL-friendly slugs from text. Common use case: generating slugs from titles for blog posts, products, etc.
Example:
class Post extends KhademModel<Post> with HasSlug {
String? title;
@override
String get slugSource => title ?? '';
}
// With observer for auto-generation:
class PostObserver extends ModelObserver<Post> {
@override
void creating(Post post) {
post.ensureSlugGenerated();
}
}
// Manual generation:
final post = Post()..title = 'Hello World!';
post.generateSlug();
print(post.slug); // "hello-world"
// Custom source:
post.generateSlugFrom('Custom Title 123');
print(post.slug); // "custom-title-123"
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasSlug → bool
-
Check if slug has been generated
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- slug ↔ String?
-
The generated slug value
getter/setter pair
- slugSource → String
-
Override this to specify the source field for slug generation
no setter
Methods
-
ensureSlugGenerated(
) → void - Ensure slug is generated (doesn't overwrite existing)
-
generateSlug(
) → void - Auto-generate slug from the slugSource
-
generateSlugFrom(
String source) → void - Generate slug from a specific string
-
getSlugWithSuffix(
int suffix) → String - Get slug with optional suffix for uniqueness
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
regenerateSlug(
) → void - Force regenerate slug from current source
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited