Saturday, May 10, 2025
News PouroverAI
Visit PourOver.AI
No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
News PouroverAI
No Result
View All Result

State Management With Provider | Kodeco

October 31, 2023
in Cloud & Programming
Reading Time: 4 mins read
0 0
A A
0
Share on FacebookShare on Twitter



Update note: Mike Katz updated this tutorial for Flutter 3. Jonathan Sande wrote the original.

Through its widget-based declarative UI, Flutter offers a simple way to build views for different states of the app. When the UI needs to reflect a new state, Flutter takes care of rebuilding the necessary components. For example, if a player scores points in a game, the label displaying the current score should update to show the new score.

To manage state changes in Flutter, you need to determine when and where to apply these changes. In an imperative environment, you might use methods like `setText()` or `setEnabled()` to modify a widget’s properties from a callback. In Flutter, however, you let the widgets know that the state has changed so that they can be rebuilt.

The Flutter team recommends using various state management packages and libraries. One of the simplest options is Provider, which allows you to update your UI when the app state changes. In this tutorial, you will learn how to use Provider with ChangeNotifier classes to update views when your model classes change. You will also learn about MultiProvider, which allows you to create a hierarchy of providers within a widget tree, and ProxyProvider, which links two providers together.

Understanding state management is crucial for becoming a proficient Flutter developer. By signing up for a Personal Kodeco Subscription, you can access the Managing State in Flutter video course, which teaches the fundamentals of state management from the ground up.

To get started, you will build a currency exchange app called Moola X. This app allows users to track various currencies and view their values in their preferred currency. Users can also keep track of their currency holdings in a virtual wallet and monitor their net worth. For this tutorial, the currency data will be loaded from a local file instead of a live service to simplify the content and focus on the Provider package. You can download the project materials by clicking the “Download materials” link at the top or bottom of the page.

After building and running the starter app, you will see that it has three tabs: an empty currency list, an empty favorites list, and an empty wallet indicating that the user has no dollars. By default, the base currency for the app is the US Dollar, but you can change it to a different currency by updating the `baseCurrency` variable in `lib/services/currency/exchange.dart`. For example, changing it to `’CAD’` will set the app to Canadian Dollars. After making the change, stop and restart the app to see the updated wallet.

Currently, the app doesn’t have much functionality. In the following sections, you will use Provider to make the app dynamic and update the UI as the user’s actions change the app’s state. The process involves an action triggering a chain of function calls that result in a state change. A Provider listens for these changes and provides the updated values to the widgets that depend on that state. By the end of the tutorial, the app will resemble the provided image.

The first step is to fix the loading of the first tab so that the view updates when the data is loaded. In `lib/main.dart`, the `MyApp` class creates an instance of `Exchange`, which is the service responsible for loading the currency and exchange rate information. When the `build()` method of `MyApp` creates the app widget, it invokes `exchange`’s `load()` method.

In `lib/services/currency/exchange.dart`, you will find the `loadCurrencies()` method, which sets off a chain of futures to load data from the `CurrencyService`. The first future, `loadCurrencies()`, fetches the currencies and updates the internal `currencies` list when the fetch completes.

Next, take a look at `lib/ui/views/currency_list.dart`. The `CurrencyList` widget displays a list of all the known currencies in the first tab. The information from the `Exchange` is passed through `CurrencyListViewModel` to separate the view and model logic. The view model class informs the `ListView.builder` on how to construct the table.

Currently, when the app launches, the `Exchange`’s currencies list is empty. As a result, the view model reports that there are no rows to build for the list view. When the data is loaded, the `Exchange`’s data updates, but there is no way to inform the view that the state has changed. Additionally, `CurrencyList` itself is a `StatelessWidget`, so the list will only show the updated data if you select a different tab and then re-select the currencies tab. Manually reloading the view is not an ideal user experience and goes against Flutter’s state-driven declarative UI philosophy.

To automatically update the view, you can use the Provider package. Provider has two main components: a Provider, which manages the lifecycle of the state object and provides it to the dependent view hierarchy, and a Consumer, which builds the widget tree using the value provided by the provider and rebuilds it when the value changes.

In the case of `CurrencyList`, you need to provide the view model object to the list so that it can consume the updates. The view model will listen for changes in the data model (the `Exchange`) and forward those values to the widgets in the view. To use Provider, you need to add it as a dependency in the project. You can do this by running the following command in the terminal from the `moolax` base directory: `flutter pub add provider`. This command adds the latest version of Provider to the `pubspec.yaml` file and downloads the package.

Now that Provider is available, you can use it in the widget. Import the package by adding `import ‘package:provider/provider.dart’;` to the top of `lib/ui/views/currency_list.dart`. Then, replace the existing `build()` method with the updated code provided. This new method demonstrates the main concepts of Provider: the Provider and Consumer. The `ChangeNotifierProvider` widget manages the lifecycle of the provided value, and the inner widget tree that depends on it gets updated when the value changes. The `create` block instantiates the view model object, and the `Consumer` widget uses the provider for `CurrencyListViewModel` and passes the provided value to the builder method. Finally, the builder method returns the same `ListView` created by the helper method as before. As the `CurrencyListViewModel` notifies its listeners of changes, the Consumer provides the new value to its children.

Note: In tutorials and documentation examples, the Consumer is often the immediate child of the Provider, but it can be placed anywhere.



Source link

Tags: KodecoManagementProviderState
Previous Post

Researchers from ISTA Austria and Neural Magic Introduce QMoE: A Revolutionary Compression Framework for Efficient Execution of Trillion-Parameter Language Models

Next Post

Protect your web apps from modern threats with Microsoft Defender for Cloud

Related Posts

Top 20 Javascript Libraries You Should Know in 2024
Cloud & Programming

Top 20 Javascript Libraries You Should Know in 2024

June 10, 2024
Simplify risk and compliance assessments with the new common control library in AWS Audit Manager
Cloud & Programming

Simplify risk and compliance assessments with the new common control library in AWS Audit Manager

June 6, 2024
Simplify Regular Expressions with RegExpBuilderJS
Cloud & Programming

Simplify Regular Expressions with RegExpBuilderJS

June 6, 2024
How to learn data visualization to accelerate your career
Cloud & Programming

How to learn data visualization to accelerate your career

June 6, 2024
BitTitan Announces Seasoned Tech Leader Aaron Wadsworth as General Manager
Cloud & Programming

BitTitan Announces Seasoned Tech Leader Aaron Wadsworth as General Manager

June 6, 2024
Copilot Studio turns to AI-powered workflows
Cloud & Programming

Copilot Studio turns to AI-powered workflows

June 6, 2024
Next Post
Protect your web apps from modern threats with Microsoft Defender for Cloud

Protect your web apps from modern threats with Microsoft Defender for Cloud

Using Crystalize.js with React for dynamic state management

Using Crystalize.js with React for dynamic state management

Dutch regulator disputes Apple’s commissions in dating app case By Reuters

Dutch regulator disputes Apple's commissions in dating app case By Reuters

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
Is C.AI Down? Here Is What To Do Now

Is C.AI Down? Here Is What To Do Now

January 10, 2024
Porfo: Revolutionizing the Crypto Wallet Landscape

Porfo: Revolutionizing the Crypto Wallet Landscape

October 9, 2023
A Complete Guide to BERT with Code | by Bradney Smith | May, 2024

A Complete Guide to BERT with Code | by Bradney Smith | May, 2024

May 19, 2024
How To Build A Quiz App With JavaScript for Beginners

How To Build A Quiz App With JavaScript for Beginners

February 22, 2024
Saginaw HMI Enclosures and Suspension Arm Systems from AutomationDirect – Library.Automationdirect.com

Saginaw HMI Enclosures and Suspension Arm Systems from AutomationDirect – Library.Automationdirect.com

December 6, 2023
Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

November 20, 2023
Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

June 10, 2024
AI Compared: Which Assistant Is the Best?

AI Compared: Which Assistant Is the Best?

June 10, 2024
How insurance companies can use synthetic data to fight bias

How insurance companies can use synthetic data to fight bias

June 10, 2024
5 SLA metrics you should be monitoring

5 SLA metrics you should be monitoring

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

June 10, 2024
Facebook Twitter LinkedIn Pinterest RSS
News PouroverAI

The latest news and updates about the AI Technology and Latest Tech Updates around the world... PouroverAI keeps you in the loop.

CATEGORIES

  • AI Technology
  • Automation
  • Blockchain
  • Business
  • Cloud & Programming
  • Data Science & ML
  • Digital Marketing
  • Front-Tech
  • Uncategorized

SITEMAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 PouroverAI News.
PouroverAI News

No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing

Copyright © 2023 PouroverAI News.
PouroverAI News

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In