TetherModel<T> class
abstract
An abstract base class for all data models managed by the Tether system.
TetherModel
provides a common interface for models that can be serialized
to and from JSON (for network communication, e.g., with Supabase) and
an SQLite-compatible format (for local database persistence).
Subclasses must implement:
toJson()
: To serialize the model to a JSON map.toSqlite()
: To serialize the model to a map suitable for SQLite insertion/update. This might involve converting complex types (likeDateTime
or enums) to SQLite-compatible types (like ISO8601 strings or integers).copyWith(...)
: A method to create a new instance of the model with some fields updated. The actual parameters forcopyWith
should be specific to the subclass and its properties.- A
static fromJson(Map<String, dynamic> json)
factory constructor (or equivalent) for deserializing from JSON. - A
static fromSqlite(Row sqliteData)
factory constructor (or equivalent) for deserializing from an SQLiteRow
.
The generic type T
should be the type of the implementing class itself, enabling
type-safe copyWith
and factory methods.
Example (conceptual subclass):
class MyData extends TetherModel<MyData> {
final int id;
final String name;
final DateTime createdAt;
MyData({required this.id, required this.name, required this.createdAt})
: super({'id': id, 'name': name, 'created_at': createdAt.toIso8601String()});
MyData.fromData(Map<String, dynamic> data)
: id = data['id'] as int,
name = data['name'] as String,
createdAt = DateTime.parse(data['created_at'] as String),
super(data);
@override
dynamic get localId => id;
@override
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'created_at': createdAt.toIso8601String(),
};
@override
Map<String, dynamic> toSqlite() => {
'id': id,
'name': name,
'created_at': createdAt.toIso8601String(), // SQLite stores as TEXT
};
@override
MyData copyWith({int? id, String? name, DateTime? createdAt}) =>
MyData(
id: id ?? this.id,
name: name ?? this.name,
createdAt: createdAt ?? this.createdAt,
);
static MyData fromJson(Map<String, dynamic> json) => MyData(
id: json['id'] as int,
name: json['name'] as String,
createdAt: DateTime.parse(json['created_at'] as String),
);
static MyData fromSqlite(Row row) => MyData(
id: row['id'] as int,
name: row['name'] as String,
createdAt: DateTime.parse(row['created_at'] as String),
);
@override
String toString() => 'MyData(id: $id, name: $name, createdAt: $createdAt)';
}
T
is the concrete model type (e.g., BookModel)
I
is the corresponding input type (e.g., BookInput)
Constructors
-
TetherModel.new(Map<
String, dynamic> data) -
Constructs a TetherModel with the given
data
map. -
TetherModel.fromJson(Map<
String, dynamic> json) -
A generic factory constructor to create a
TetherModel
instance from a JSON map.factory - TetherModel.fromSqlite(Row sqliteData)
-
A generic factory constructor to create a
TetherModel
instance from an SQLiteRow
.factory
Properties
-
data
→ Map<
String, dynamic> -
The underlying data map holding the model's properties.
Subclasses should initialize this in their constructors.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- localId → dynamic
-
Provides access to the local identifier of the model instance.
This typically corresponds to the primary key in the local SQLite database
(e.g., 'id').
Subclasses should override this if their primary key column is named differently.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toInput(
) → TetherModelInput< TetherModelInput, dynamic> - Creates a corresponding input object for this model.
-
toJson(
) → Map< String, dynamic> - Serializes the model instance to a JSON-compatible Map.
-
toSqlite(
) → Map< String, dynamic> - Serializes the model instance to a Map suitable for insertion or update into an SQLite database.
-
toString(
) → String -
Creates a new instance of the model
T
with updated properties.override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited