InteractsWithPersistence mixin

The Persistence Concern.

This mixin provides Active Record pattern methods for hybrid persistence. Models can be persisted to local SQLite database, remote API, or both.

Usage

class User extends Model with HasTimestamps, InteractsWithPersistence {
  @override String get table => 'users';
  @override String get resource => 'users';
}

// Find a user
final user = await User.find<User>(1);

// Get all users
final users = await User.all<User>();

// Save changes
user.name = 'New Name';
await user.save();

// Delete
await user.delete();

Hybrid Persistence

By default, models use both local and remote persistence. Configure this with useLocal and useRemote:

class ApiOnlyModel extends Model with InteractsWithPersistence {
  @override bool get useLocal => false;
  @override bool get useRemote => true;
}
Superclass constraints

Properties

appends → List<String>
The accessors to append to the model's array form.
no setterinherited
attributes → Map<String, dynamic>
Get all attributes as a Map.
no setterinherited
casts → Map<String, dynamic>
The attributes that should be cast.
no setterinherited
exists ↔ bool
Indicates if the model exists in the database.
getter/setter pairinherited
fillable → List<String>
The attributes that are mass assignable.
no setterinherited
guarded → List<String>
The attributes that are guarded from mass assignment.
no setterinherited
hashCode → int
The hash code for this object.
no setterinherited
hidden → List<String>
The attributes that should be hidden for serialization.
no setterinherited
id ↔ dynamic
Get the model's primary key value.
getter/setter pairinherited
incrementing → bool
Indicates if the primary key is auto-incrementing.
no setterinherited
primaryKey → String
The primary key for the model.
no setterinherited
relations → Map<String, Model Function()>
The model relationships for automatic casting.
no setterinherited
resource → String
The API resource name for remote operations.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
table → String
The table associated with the model.
no setterinherited
useLocal → bool
Whether to use local database persistence.
no setterinherited
useRemote → bool
Whether to use remote API persistence.
no setterinherited
validationErrors → Map<String, List<String>>
The per-field validation errors from the most recent save.
no setter
visible → List<String>
The attributes that should be visible for serialization.
no setterinherited
wasRecentlyCreated ↔ bool
Indicates if the model was recently created.
getter/setter pairinherited

Methods

append(List<String> attributes) → Model
Append accessors to the model.
inherited
delete() → Future<bool>
Delete the model from the database and/or remote API.
fill(Map<String, dynamic> attributes, {bool strict = false}) → void
Fill the model with an array of attributes.
inherited
get<T>(String key, {T? defaultValue}) → T?
Get an attribute value with optional default and type casting.
inherited
getAttribute(String key) → dynamic
Get an attribute from the model.
inherited
getDirty() → Map<String, dynamic>
Get the dirty attributes (those that have changed).
inherited
getRelation<T extends Model>(String key) → T?
Get a single related model.
inherited
getRelations<T extends Model>(String key) → List<T>
Get a list of related models.
inherited
has(String key) → bool
Check if an attribute exists and is not null.
inherited
isDirty([String? attribute]) → bool
Create a model instance from a Map.
inherited
makeHidden(List<String> attributes) → Model
Make attributes hidden.
inherited
makeVisible(List<String> attributes) → Model
Make attributes visible.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
query() → QueryBuilder
Get a query builder for the model's table.
refresh() → Future<bool>
Refresh the model from the database/API.
save() → Future<bool>
Save the model to the database and/or remote API.
set(String key, dynamic value) → void
Set an attribute value.
inherited
setAttribute(String key, dynamic value) → void
Set an attribute on the model.
inherited
setRawAttributes(Map<String, dynamic> attributes, {bool sync = false}) → void
Set the model's raw attributes.
inherited
syncOriginal() → void
Sync the original attributes with the current.
inherited
toArray() → Map<String, dynamic>
Alias for toMap - Laravel-style naming.
inherited
toJson() → String
Convert the model to a JSON string.
inherited
toMap() → Map<String, dynamic>
Convert the model to a Map.
inherited
toString() → String
A string representation of this object.
inherited
updateTimestamps() → void
Update the model's timestamps.
inherited
validationError(String field) → String?
The first validation message for field, or null when field has none.

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

allModels<T extends Model>(T factory()) → Future<List<T>>
Get all models.
findById<T extends Model>(dynamic id, T factory()) → Future<T?>
Find a model by its primary key.
hydrate<T extends Model>(Map<String, dynamic> data, T factory()) → T
Create a new model instance from raw attributes.