Data Models
Data models are essential building blocks when you're working with structured data in your app. In Nowa, you can visually create and manage data models that represent real-world objectsβlike a task, a product, a user profile, or a log entryβand then use them across your app in variables, logic, and UI.
π Why You Need Data Modelsβ
Here are common scenarios where creating a model is necessary:
- To-do apps: Each task needs a structure like title, description, and status.
- E-commerce apps: A product has properties like name, price, and image.
- User management: A user might have a name, email, and role.
- Water tracker app: Each water log entry can have an amount and timestamp.
Creating models helps you avoid repeating data structures manually and makes your app more scalable and easier to maintain.
β¨ How to Create a Data Modelβ
-
Click on the β next to the
lib
folder in the Files panel. -
Choose "Create new object".
-
A popup will appear. Enter the model name (e.g.,
Task
).At the bottom of the popup:
Path:
shows where the Dart file will be created.Class:
shows the Dart class name youβll use in your logic and variables.
-
After confirming, the model will appear in the Files panel as a Dart file.
The file and class names are automatically adjusted:
- A model named
"task model"
becomes:- Class name:
TaskModel
(PascalCase) - File name:
task_model.dart
(snake_case)
- Class name:
This follows Dartβs best practices.
βοΈ Adding Variables and Functionsβ
- Single-click the model file to open a popup showing
variables
andfunctions
. - Click β next to either section to create a new variable or function.
In the popup:
- Choose the variable name and type.
- Set a default value (optional).
- Enable
is final
if the variable should not change after being set.
final
Use final
when:
- The value shouldnβt change, like
id
,createdAt
.
Avoid final
when:
- You want to update it later, like
title
,description
,isDone
.
In the video above, we:
- Created
title
,description
, andisDone
variables for theTask
model. - Set all as non-final so users can update them.
- Added a method
markAsComplete
to mark a task as done.
π§βπ» View and Edit the Model Codeβ
- Double-click the model file to open its Dart code.
- You can here view the source-code of the model, or edit or add variables and functions here directly. To do so:
- Double-click the file name.
- Click β next to the model name to add or edit variables/functions.
In the video, we:
- Opened
task.dart
. - Added a new variable
priority
of typeint
. - Instantly saw the code update.
π Using Data Modelsβ
1. As a Data Typeβ
You can use your model as a custom type for cases like:
- Variables
- Parameters
- Function parameters and returns
Example:
- We created a list variable
taskList
of typeList<Task>
. - Set a default value with 3 tasks.
- Modified each taskβs properties (title, description, etc.).
The default value is used only when no other value is set during runtime. Itβs great for testing your UI layout.
2. Creating Instances in Circuitβ
In the logic editor (Circuit):
- Use the "Create" node.
- Choose the model you want to instantiate.
- Fill in its properties.
Example:
- In the
On Pressed
event of a β button:- Created an instance of
Task
. - Set values for
title
,description
, andisDone
. - Stored it in a variable
task
. - Added it to the
taskList
.
- Created an instance of
To use dynamic values (like user input):
- Click on a property (e.g.
title
) in the instance node. - Open the linking menu. Choose what you want to link it withβit can be a variable, a parameter, the result of a function call, an expression, and more.
For example, if you have a TextField widget with a controller variable named titleTextFieldController
, and you want to use the entered text as the title
of the Task
instance, open the linking menu, then go to locals
> titleTextFieldController
> text
to bind the text input of the TextField to be the title of the task created
We used a model WaterLogEntry
to represent a log of water intake, with:
amount
: The amount of the intake asint
date
: the date where the instance was created, from the typeDateTime
.
We then created variables and used this model in the logic and UI.
Play this tutorial that will start from 6:00 to see how an object was created and used during building a Water Tracker App.
Data models in Nowa make your app logic cleaner, more powerful, and easier to scale. Whether you're building a todo list, an e-commerce app, or a fitness tracker, models are your best friend to organize and work with structured data.