aaaa12345
loading
It is a condensed emotional expression of the corporate policy

Build the Apple Stocks App Using Flutter (Part 1)

In the most recent post on SwiftUI, we learned how to create the Apple Stocks App in the SwiftUI framework.Since Flutter and SwiftUI share a lot of similarities, we thought it would be a good idea to implement the same app using the Flutter framework.In this multi-part post, we will learn how to create the home page which displays stocks to the user.

In a future post, we will cover how to implement the news listing page for the Flutter stocks app. Take a look at the screenshot of the finished app.Tour of the ServerOur server is implemented in Node using the Express.

js package and returns hard-coded stocks as JSON. The complete implementation of the server is shown below:Nothing fancy!Fetching Stocks From JSON Web APILets start by implementing a client that will fetch data from the JSON API. The client will have a single method to get all the stocks and returns the response as a future.

We are using the HTTP package to perform the networking request but you are more than welcome to use any other HTTP library. The implementation of the web service is shown below:The web service fetches the stocks from our custom server and then populates the Stock model, which is later returned to the caller as a Future>.The Stock model is implemented below:The fromJson factory method takes in a Map object and then returns a new instance of Stock based on the JSON data.

Next, we need to get the stocks data displayed on the page. Before we jump into the user interface implementation, we need to implement view models that can hold and supply the data to the view.Implementing View ModelsView models are responsible for carrying the data and then binding it to the view. A view model can represent an entire page, even containing child view models to target parts of the page.The implementation of view models is shown below:The StockListViewModel represents the entire page and the StockViewModel represents individual stock cells that will be displayed in a list.The fetchStocks method is responsible for using the Webservice instance and retrieving all the stocks.

Once the stocks are fetched, the result is transformed into an array of StockViewModel objects and assigned to the stocks property of StockListViewModel.The StockListViewModel also extends the ChangeNotifier class, enabling it to notify listeners. Once the notifyListeners method is fired, all the listeners are notified and they can take action.

For our case, we want to update the view so it can show the latest stocks.In order to get a change notification from the change notifier, you will have to use ChangeNotifierProvider. ChangeNotifierProvider is part of the provider package. The pubspec.

yaml is updated to add the provider package.dependencies:flutter:sdk: flutter# The following adds the Cupertino Icons font to your application.# Use with the CupertinoIcons class for iOS style icons.

cupertino_icons: ^0.

1.

2 ^0.

12.

04provider: ^4.

0.

1Next, lets see how we can pass the StockListViewModel instance to the view.Displaying Stocks on the ViewIn order to display stocks, we have to access the StockListViewModel inside our view. We can get access to the view model using the ChangeNotifierProvider.

We are going to inject the instance of StockListViewModel to our home page widget. This is implemented in the main.dart file as shown below:The above code makes sure that the StockListViewModel instance is available to HomePage and all its child widgets.Inside the HomePage widget, we use the Provider.

of to get an instance of StockListViewModel and then we call the fetchStocks function on the view model.The fetchStocks will retrieve the stocks from the Web API and populates the stocks property of the StockListViewModel.Once the stocks property is set we call the notifyListeners method which refreshes the view using the Consumer widget.

The StockList widget is responsible for displaying a list of stocks. The StockList widget takes a single argument, which is the list of stocks.If you want to learn more about data flow in Flutter apps then check out this article. The implementation of the StockList widget is shown below:If you run the app, you will be able to see the Stocks app in action.Searching StocksAt present, typing in the text field does not do anything. We need to filter the stocks based on the input from the user. The search functionality is implemented in the StockListViewModel as shown below:Inside the search method, we use the where feature to filter out the stocks based on user input.Thats it!ConclusionI really hope you have enjoyed the article. In the second part, I will cover how to display news from a live Web API and add drag gestures to swipe/down the news view.

ResourcesGitHubBuilding Apple stocks in Flutter videos

·RELATED QUESTION

OK....somebody clever and crafty please give me your ideas!!?

How about Floam or Silly Puddy?

Or those balls you can get that are like the ones they put in the play gyms at like MCDonald's? Yellow, blue, and red? You can takje the red ones, draw a stem on them and draw a face to be cute.

GET IN TOUCH WITH Us
recommended articles
wen
I had a neighbor who had dentures since she was 18. Her own teeth were so crooked and her parents could not afford braces so she had them pulled. She loved her new '...
The suspect in the Pennsylvania police barrack ambush last week was added to the FBI's 10 most wanted list Friday as the search focuses in on the wooded area in the ...
The wiring diagram of single chip microcomputer 80C51 is shown in Figure 1. In Figure 1, position 4 shows the common anode for the tube. Use dynamic display and cycl...
Principle of three-stage dimming of the lamp_How to use TRIAC to dim the LED lamp and how to design the specific scheme? - Programmer SoughtAt present, non-energy-sa...
"To talk about smart grid, Jiangning District has gathered more than 300 enterprises, led by 12 listed enterprises such as NARI Group and Guodian Nanzi, covering the...
What is your policy on bath toys?We only have as many bath toys as can fit in one regular-sized mesh bag on the shower wall. I do not want them taking over the bathr...
Well graphic cards are not cheap, but you will always get ripped by people taking a look at your computer, there's no way around that unless you fix it yourself or h...
No it would not , as a pure black light bulb will not allow the light to come out, and it means it is same when it is on or off. So neither it makes the room lighter...
How to Repair Rain GuttersThis post may contain affiliate links. For more information see our disclosures here . Expert advice on downspout and gutter repairs. Stop ...
(source: Robert Institute of robotics, China)Yaskawa motoman-gp7 is a 6-axis vertical multi joint robot. Because of its combination with the small robot controller "...
no data
ADDRESS
Manhatthan
NY 1234 USA
master@weyes.cn
LINKS
Home
Services
Portfolio
Career
Contact us
PRODUCT
Chandelier
Wall Lamp
Table Lamp
Floor Lamp
Contact Us
+86 020-22139352
If you have a question, please contact at contact service@lifisher.com
Copyright © 2025 | Sitemap
Contact us
whatsapp
phone
email
contact customer service
Contact us
whatsapp
phone
email
cancel
Customer service
detect