aaaa12345
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
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.