-
Notifications
You must be signed in to change notification settings - Fork 3
RocketModel
Mohammed CHAHBOUN edited this page Jan 20, 2026
·
1 revision
RocketModel is the base class for all your data models. It provides built-in state management, JSON serialization, and notification logic.
A RocketModel typically consists of:
- Properties: Your data fields.
- Field Constants: String keys matched to JSON keys.
- fromJson: Logic to populate fields from a Map.
- updateFields: Logic to update specific fields and trigger selective rebuilds.
import 'package:flutter_rocket/flutter_rocket.dart';
// 1. Define field keys as constants for reliability
const String userIdField = "userId";
const String idField = "id";
const String titleField = "title";
class Post extends RocketModel<Post> {
// 2. Properties
int? userId;
int? id;
String? title;
Post({this.userId, this.id, this.title});
// 3. Populate from JSON
@override
void fromJson(Map<String, dynamic>? json, {bool isSub = false}) {
if (json == null) return;
userId = json[userIdField];
id = json[idField];
title = json[titleField];
super.fromJson(json, isSub: isSub);
}
// 4. Update fields and trigger selective rebuilds
void updateFields({int? userIdField, int? idField, String? titleField}) {
List<String> fields = [];
if (userIdField != null) {
userId = userIdField;
fields.add(userIdField);
}
// ... add logic for other fields
rebuildWidget(fromUpdate: true, fields: fields.isEmpty ? null : fields);
}
@override
Post get instance => Post();
}Every RocketModel has a state property of type RocketState:
-
RocketState.loading: Data is being fetched. -
RocketState.done: Data is loaded successfully. -
RocketState.failed: An error occurred.
You can check the state in your UI to show loaders or error messages.
When you fetch a list of items, the all property of the RocketModel holds the list. It is also an instance of RocketModel (or a subclass) that manages the list state.
final Post posts = Post();
// After fetching...
print(posts.all!.length);- Use Rocket CLI to generate these models instantly.
- Always use constant strings for field names to avoid typos.
- Override
enableDebugtotrueduring development to see state transition logs.