State Management in Nylo 5 | Flutter Framework

Nylo
5 Dec 202311:07

Summary

TLDRIn this video, the host introduces the concept of State Management, focusing on its use in managing the state of a widget in a Nano project. The video demonstrates building a simple shopping cart application with two pages (product detail and account), showcasing how State Management works under the hood. The host walks through creating widgets, updating state across multiple widgets, and handling state persistence through local storage. Viewers learn how to use state functions, update widgets dynamically, and implement controllers for better state management across the app.

Takeaways

  • πŸš€ The video introduces state management in the context of a Nano project, focusing on its application and benefits.
  • πŸ›’ The example used is a shopping cart widget, demonstrating how state management can dynamically update the number of items in the cart across different pages.
  • πŸ“ State management is crucial for maintaining and updating the state of a widget without needing to reload the entire page.
  • πŸ’» The video provides a step-by-step guide on creating a state-managed widget using the command 'state for widget' in the console.
  • πŸ”— The 'init' function is highlighted as essential for initializing the widget's state when the app first starts.
  • πŸ”„ The 'state updated' function is key for handling updates to the widget's state from other parts of the application.
  • πŸ“Š The video shows how to update the state from different sources, including directly from a variable and from local storage.
  • πŸ”— The 'update state' function is used to update the state, which can be triggered from various parts of the application, such as a controller.
  • πŸ› οΈ The script includes a practical demonstration of creating a button that updates the shopping cart's state, showcasing the reactivity of state management.
  • 🌐 The video concludes by emphasizing the ease of creating state-managed widgets in Nano and the flexibility it offers in managing app state across different components.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is State Management in the context of a Nano project, specifically how to use it to manage the state of a widget.

  • What is State Management and why is it important?

    -State Management is a system that allows developers to manage and track the state of a widget within an application. It's important because it helps in maintaining and updating the state across different parts of an application without needing to reload the page.

  • What does the video suggest to do before starting with State Management?

    -The video suggests downloading the Nano project from N.D to get started with State Management.

  • How many pages does the example application in the video have?

    -The example application in the video initially has two pages: a product detail page and an account page.

  • What is the purpose of the shopping cart widget created in the video?

    -The shopping cart widget is created to display an icon representing the number of items in the cart and to demonstrate how State Management works across different pages of an application.

  • What command is used to create a state-managed widget in the video?

    -The command used to create a state-managed widget is 'state for widget' followed by the widget's name, in this case, 'shopping cart icon'.

  • How does the video demonstrate updating the state of the shopping cart widget?

    -The video demonstrates updating the state by using the 'updateState' function within the widget and passing in the new state data, which then updates across all instances of the widget without reloading the page.

  • What role does the 'init' function play in the state management example?

    -The 'init' function plays a crucial role in initializing the state of the widget when it is first created, setting the initial value that the widget will use.

  • How is the state of the shopping cart widget updated from the account page in the video?

    -The state of the shopping cart widget is updated from the account page by calling the 'updateState' function and passing in the new quantity, which then reflects across all instances of the widget.

  • What is the significance of the 'State data' variable mentioned in the video?

    -The 'State data' variable holds the last value that was set to the widget, allowing the widget to retrieve and display the most recent state even after the application has been navigated away from and returned to.

  • How does the video show the use of local storage for state management?

    -The video demonstrates using local storage for state management by saving the cart's quantity to a collection in local storage and then updating the state of the widget to reflect the changes stored there.

Outlines

00:00

πŸ›’ Introduction to State Management in N

The video begins by welcoming viewers back to the channel and introducing the topic of State Management, which has been highly requested. The host explains that State Management is about managing the state of a widget and that N provides an easy way to do this. They encourage viewers to download the N project from N.D to get started. The video then demonstrates how to build a simple application with a shopping cart feature, which includes a widget displaying the number of items in the cart. The host guides viewers through creating two pages, a product detail page, and an account page, and then adds a state-managed widget to these pages. The widget is created using a command alias 'Metro', which simplifies the process. The video concludes this section by showing the initial setup of the application with the shopping cart widget displayed on both pages.

05:01

πŸ”— Linking Pages and State Management

The second paragraph delves into linking the product detail page to the account page and creating a simple UI with a button that navigates to the account page. The host then focuses on State Management, explaining how to update the state of the shopping cart widget from another widget without reloading the page. They introduce a new variable 'C value' and demonstrate how to use the 'state updated' function to update the state. The video shows how changes made on the account page are reflected in real-time on the product detail page, thanks to State Management. The importance of the 'init' function for initializing the widget's state and the 'state updated' function for handling state changes are emphasized. The host also illustrates how to persist the state across page navigations by using the 'State data' variable.

10:02

πŸ’Ύ Updating State from Local Storage

In the third paragraph, the host explores updating the state from local storage rather than from a variable. They introduce a 'cart storage key' in the config section, which is responsible for storing the cart's value. The video demonstrates how to implement this storage key and update the 'init' function to retrieve the state from local storage. The host then shows how to save the cart's state to local storage using a button and a function that writes to the collection. They explain that the state is managed outside the class and can be updated from any part of the project, such as a provider or a controller. The video concludes with a demonstration of updating the quantity from a controller, showing the power and ease of managing state in N.

Mindmap

Keywords

πŸ’‘State Management

State Management refers to the process of managing the state of a widget or application, ensuring that changes in one part of the application are reflected across all relevant components without the need for manual updates. In the video, State Management is central to the theme as it allows for dynamic updates of the shopping cart icon's state across different pages of the application, such as the product detail and account pages, without reloading the entire page.

πŸ’‘Widget

A widget in the context of the video represents a reusable component within a user interface. The video focuses on creating a 'shopping cart icon' widget, which displays the number of items in the cart. This widget is state-managed, meaning its state can be updated and reflected across the application, demonstrating the utility of widgets in creating modular and maintainable UI components.

πŸ’‘N.D

N.D is mentioned as a platform or framework where the project is being developed. It seems to be a part of the development environment where the state management and widget creation are being demonstrated. The video encourages viewers to download the project from N.D to follow along, indicating that it's a key resource for the tutorial.

πŸ’‘Metro

Metro is introduced as an alias for a longer command used in the development environment. It simplifies the process of creating state-managed widgets by allowing developers to use a shorter command instead of a full one. This is showcased in the video where the presenter uses 'Metro' to create a shopping cart widget, highlighting the efficiency it brings to the development process.

πŸ’‘State Updated Function

The 'state updated function' is a crucial part of the state management process in the video. It is used to update the state of a widget from another part of the application. For instance, the video demonstrates how to update the shopping cart icon's state from the account page, showing how this function allows for state changes to be propagated across the application seamlessly.

πŸ’‘LocalStorage

LocalStorage is a web storage mechanism that allows data to be stored locally within the user's browser. In the video, LocalStorage is used to persist the state of the shopping cart across sessions. The script shows how to save the cart's state to LocalStorage and retrieve it when the application is loaded, ensuring that the user's cart remains consistent even after the browser is closed and reopened.

πŸ’‘Controller

A controller in the context of the video is a component that handles the logic and data flow of the application. The video demonstrates moving the state update logic into a controller, which then manages the state updates for the shopping cart widget. This illustrates the concept of separating the UI from the business logic, a common practice in modern application development for better maintainability and scalability.

πŸ’‘Product Detail Page

The 'Product Detail Page' is one of the pages created in the application to demonstrate state management. It is where users can view details of a product and is equipped with the shopping cart icon widget. The video uses this page to show how state changes in one part of the application, such as updating the cart's item count, can be reflected in real-time on this page without a page reload.

πŸ’‘Account Page

The 'Account Page' is another page in the application used in the video to illustrate state management. It is where users can manage their account settings, including updating the quantity in their shopping cart. The video demonstrates how changes made on the Account Page, such as updating the cart's quantity, are instantly reflected on the Product Detail Page, showcasing the cross-page state synchronization capabilities.

πŸ’‘Init Function

The 'init function' is mentioned in the context of initializing the state of a widget when it is first created. The video explains that this function is important for setting the initial state of the shopping cart icon from LocalStorage. It is used to retrieve the last known state of the widget, ensuring that the application starts with the correct data, even after a browser refresh.

Highlights

State Management allows managing the state of a widget easily in NLO.

The shopping cart icon is created as a state-managed widget and displayed on both product detail and account pages.

Using Metro simplifies command execution for project management in NLO.

State Management enables seamless updating of widget data without reloading pages.

State updated function is used to update the state of a widget dynamically from another widget.

State data retains the last value set to the widget, allowing continuity between page navigations.

The account page allows updating the shopping cart quantity, reflecting the changes in both pages.

Widgets can display in multiple locations simultaneously, reflecting synchronized state updates.

The init function is crucial for initializing widget state during the first app launch.

State Management is extended with local storage to persist shopping cart data across sessions.

The cart data is saved in local storage using a unique key and retrieved during widget initialization.

Local storage integration enables persistent state even after app restarts, enhancing user experience.

The update quantity function is abstracted into a controller, separating logic from the UI components.

The state can be managed outside the widget, like in controllers or providers, for modular design.

Creating a state-managed widget is simplified with the make command in NLO, which handles initialization and customization.

Transcripts

play00:00

hello and welcome back to the channel in

play00:02

today's video we're going to take a look

play00:04

at State Management uh this has been

play00:06

quite a requested topic on the channel

play00:07

so we'll do our best to explain what it

play00:10

is and how you can use it in your Nano

play00:12

project so just to give you a recap or

play00:16

an overview of what it is um State

play00:18

Management allows us to manage the state

play00:20

of a widget and um in N under the hood

play00:24

we have an easy way for you to do that

play00:26

so if you haven't already definitely

play00:28

head over to N.D and download the

play00:30

project to get started and if we head

play00:33

over to the project uh and just build

play00:35

and run you should get the same example

play00:37

here so uh with that done um let's take

play00:41

a look at like an example so let's build

play00:44

an application that has a shopping cart

play00:47

and this shopping cart is going to have

play00:49

a icon of how many items are actually in

play00:52

the cart and we'll just have two pages

play00:55

at the moment just to kind of

play00:57

demonstrate how it works and we're go

play01:00

into some more examples in the video but

play01:03

yeah let's first create those pages so

play01:05

from the console let's just create our

play01:07

first page so we'll create like a

play01:10

product detail

play01:11

page and then let's also create a

play01:14

account page as well like an account

play01:16

page and then the next thing we'll do is

play01:18

create our widget so this is going to be

play01:20

State managed in NLO uh all we need to

play01:23

do is run state for widget and then yeah

play01:27

we can just add the name so this is

play01:29

going to be the shopping cart Widget

play01:32

icon um and you can see it's created

play01:34

that for us here just by the way if

play01:36

you're wondering what this is Metro um

play01:39

Metro is essentially uh this long

play01:41

command here um you can actually just

play01:43

run the same commands from the console

play01:45

using this but uh I've created an alias

play01:48

so that I can just use Metro um there's

play01:51

some documentation on the N website if

play01:53

you want to just use Metro like I am in

play01:55

the video so yeah let's start adding the

play01:58

widget now to our page pages so this

play02:01

icon is going to be displayed in the

play02:03

yeah top left hand corner so let's go

play02:06

over here and just like add it as like

play02:09

an action so here we can just add like

play02:12

shopping

play02:13

cart there we go and yeah we'll just do

play02:16

the same for the other page so on the

play02:18

account page we're just going to also

play02:23

add shopping

play02:26

cart just like

play02:28

that so what we'll do is we'll set the

play02:31

product detail page as the initial page

play02:33

so if we build and run now you should

play02:35

see yeah the product detail page and we

play02:38

also have our widget being displayed in

play02:40

the top right hand corner but uh yeah

play02:43

let's just customize the look of this so

play02:45

it will be like a really simple

play02:52

widget so from the product detail page

play02:55

what we also want to do is connect it to

play02:57

the account page so we created a page

play03:00

previously here account page and what

play03:03

we'll do is just create like a simple UI

play03:06

um I'm just going to create like one

play03:08

button just say go to let me just call

play03:11

it

play03:12

account account

play03:14

page and then we'll just do

play03:18

untap and then we'll just route to the

play03:20

account page so if we just try this you

play03:24

can see it takes us to the account page

play03:26

and we can also come back as well so you

play03:29

can see that's working and we also

play03:31

seeing the other widget that we created

play03:33

so the widgets are um both the

play03:37

same so now let's take a look at some

play03:39

State Management so inside our widget uh

play03:42

at the moment this is hardcoded and if

play03:44

we set it to five you see updates and if

play03:47

we do it to one um but what we want is

play03:49

to update it from another widget so what

play03:53

we can do is use the state updated

play03:55

function here and this is where all the

play03:58

magic happens so

play04:00

uh let's create a new variable inside

play04:02

here let's just call it C value and

play04:04

let's just set this equal to zero for

play04:07

now and inside here what we're going to

play04:09

do is just set the the value equal to

play04:11

data for now and we'll just update this

play04:16

equal to that

play04:18

value just do a pass and say it like

play04:22

that so at the moment it's working as

play04:25

you can see we have zero here and if we

play04:27

go to the account page we also have zero

play04:31

but um yeah let's imagine we want to

play04:33

update the cart now from the account

play04:35

page and we want also and we also wanted

play04:38

to update the product detail page

play04:41

without having to reload the whole page

play04:44

so um let's take a look at that example

play04:47

so on the account page here let's just

play04:50

add a new

play04:54

button so GitHub copilot helped me out a

play04:57

little bit there but um essentially

play04:59

what's important is that you use the

play05:01

update State function like this and then

play05:04

just pass in the state that you want to

play05:05

update so this will be for your widget

play05:07

so we just need to pass in yeah shopping

play05:09

car icon. state and then here where

play05:12

we're setting the data so let's just try

play05:16

that so yeah if we go to the account

play05:18

page and update quantity you'll see we

play05:20

get two if we head back you'll see that

play05:22

we also see two as well so that's

play05:24

matching up but um if we go to the

play05:27

account page again you'll see that that

play05:29

value is now reset back to zero and

play05:32

that's because we're not setting this

play05:34

value um so so in this init function

play05:39

what we need to do is call State data

play05:41

like this and uh State data is going to

play05:44

contain the last value that was set to

play05:46

the widget so in our case it would be

play05:49

like number two if we build and refresh

play05:51

and we set this to two now and then if

play05:54

we go back to the page you can see now

play05:57

it's setting the data correctly uh if we

play06:00

just print this out just to show

play06:03

you uh run it in the console let's just

play06:06

build and run so at the start we have

play06:09

null this is the first time the user

play06:11

would boot up our app this value would

play06:13

be null um cuz we haven't set any data

play06:16

to it but if we go to the account page

play06:19

and then just update the quantity so now

play06:21

it's two go back and head over again now

play06:25

you can see it's setting equal to two so

play06:28

that's all working

play06:30

um and yeah that's like one example of

play06:33

using State Management in N so from any

play06:36

widget now uh head back over to the

play06:38

account page and set this equal to let's

play06:40

just do like 9 or eight uh update

play06:43

quantity you can see update across our

play06:45

widgets and we could display this widget

play06:47

in as many places as we wanted we could

play06:50

even add multiple of the same widget

play06:52

here uh just to go a bit crazy and um

play06:55

let's just update the quantity one more

play06:57

time so let's just do it hardcoded for

play07:01

now like this and if we update four and

play07:05

you can see it's updated the state of

play07:07

all those widgets so yeah this is kind

play07:09

of like an easy example just to help you

play07:11

see how it works um the inip function is

play07:15

important for when the widget is first

play07:17

initialized for the first time and state

play07:20

updated is important when you're using

play07:22

the update State function cuz uh any

play07:25

data that you pass into here is going to

play07:27

be passed into here and that value can

play07:30

also be retrieved using the data uh the

play07:33

state data variable there so let's take

play07:35

a look at another example for updating

play07:37

State um let's update it from local

play07:40

storage instead of getting the data from

play07:42

this variable um what we're going to do

play07:45

is just go over to config so in here

play07:47

I've created a new cart storage key and

play07:50

all it's going to do is be responsible

play07:51

for storing the the value for the cart

play07:55

so uh let's just Implement

play07:58

that

play08:01

so with that done I'm also just going to

play08:02

update the init function as well because

play08:05

this is going to have to change slightly

play08:07

since we're getting it from local

play08:08

storage now so here what we'll do is

play08:11

just do the exact

play08:12

same and uh that's all we need to do so

play08:16

now we can just start adding stuff to

play08:18

our local

play08:19

storage um and we'll just keep the same

play08:21

Button as well so let's go head over to

play08:24

here and instead of using data like we

play08:28

are here

play08:29

let's just write a quick function to

play08:31

save it to the

play08:34

collection okay so with this done um

play08:37

anytime we click the button now we're

play08:39

going to be saving this object to the

play08:42

collection that is in local storage and

play08:45

essentially it's going to build up an

play08:47

array of this this object so it's going

play08:51

to end up something like this but yeah

play08:54

it'll be one at a time so and below here

play08:56

we're just updating the state like

play08:58

before but this time we're not passing

play08:59

in that data parameter we're just

play09:01

calling update State and all that's

play09:03

going to do is just call the exact same

play09:06

function here so uh let's give that a go

play09:08

and see what

play09:11

happens so on the account screen if we

play09:13

update cart and the moment we have one

play09:16

now we have two if we go back you can

play09:19

see we have two as well if we head back

play09:21

over to the account page we still have

play09:23

two so the state is being managed uh

play09:26

from outside the class and um it makes

play09:29

you can kind of see how easy it is to to

play09:31

handle that and for example this code

play09:34

could be anywhere in your project so it

play09:36

could be in one of your providers it

play09:38

could be in a controller we could do

play09:41

that now as well just create a quick

play09:45

controller and yeah let's just add that

play09:49

controller and now let's just move this

play09:51

piece of code into the

play09:55

controller so let's call it

play09:57

update quantity

play10:02

so yeah this will be doing the exact

play10:03

same as what we was doing before but

play10:05

we're just doing it from a controller

play10:06

now so now we can just run on uh

play10:11

widget controller just say update

play10:14

quantity like so and if we build and

play10:18

refresh over here update quantity you

play10:21

see it's been updated and that's all

play10:23

happening from our controller which is

play10:25

outside of our widget so um you can see

play10:29

updated here as well so really powerful

play10:32

and really easy to kind of get started

play10:35

um so in N the easiest way to create a

play10:38

state manage widget is just by calling

play10:40

make and then state for widget and then

play10:43

here we can specify the name of it just

play10:45

like we did before uh let's just call it

play10:48

maybe like like icon like that and then

play10:51

it's just going to appear in your

play10:53

resources. widgets like so and you can

play10:56

see we get the same structure as we did

play10:58

before so so we can start to then

play11:00

customize uh what happens when the state

play11:03

is updated but yeah I hope you've

play11:05

enjoyed and I'll see you in the next one

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
State ManagementN ProgrammingApp DevelopmentWidget StateShopping CartUI DesignCoding TutorialState UpdateLocal StorageCross-Widget Sync