Your TabData class has THREE main sections. Let's understand what each one is called and what it does:
Declares the class and its properties (variables)
The default way to create TabData objects
A special way to create TabData from JSON
These are called properties, fields, or instance variables.
They define what data each TabData object will store.
| Line | Property Name | Type | Can be null? |
|---|---|---|---|
| 2 | id |
String | ❌ No |
| 3 | title |
String | ❌ No |
| 4 | url |
String | ❌ No |
| 5 | favicon |
String | ❌ No |
| 6 | lastAccessed |
DateTime | ❌ No |
| 7 | isPinned |
bool | ❌ No |
| 8 | type |
String | ❌ No |
| 9 | visitCount |
int? | ✅ Yes (the ? means nullable) |
| 10 | folder |
String? | ✅ Yes (the ? means nullable) |
Let's break this into 4 parts:
Closes the parameter list
Starts the initializer list
The initializer list runs BEFORE the object is created. It's for special initialization logic.
Left side: lastAccessed → the property (same as this.lastAccessed)
Right side: lastAccessed → the parameter from line 17
?? = "if null, use this instead"
DateTime.now() = current date/time
"Set this.lastAccessed (the property) to the value of lastAccessed (the parameter), BUT if the parameter is null, use DateTime.now() instead"
Keyword that makes this a factory constructor
What's special about factory? It can do logic/processing BEFORE creating the object. It can also return an existing object instead of creating a new one (though we don't do that here).
This is a named constructor
Why "fromJson"? Because this constructor creates a TabData object FROM JSON data. You can have multiple constructors with different names!
This is the parameter - a Map (like a dictionary)
=> is called an "arrow function" or "expression body"
Meaning: "return the result of TabData(...)"
This factory constructor calls the regular constructor (Part 2) and returns the result!
Each line extracts data from the json Map and passes it to the regular constructor: