This is an example of a Flutter app that lists stock information obtained from a public API. The app displays the stock symbol, the sector it belongs to, and the latest price.
-
Package Imports: The code begins by importing necessary packages for Flutter, including the
http
package for making HTTP requests and thedart:convert
package for JSON data manipulation. -
Main Class (
MyApp
): TheMyApp
class is the entry point of the app. It returns an instance ofMaterialApp
, which is the root of the Flutter UI. -
Stock Listing Page (
StockListPage
): This is the main page of the app, which extendsStatefulWidget
to allow dynamic state updates. It contains a list of stocks. -
Page State for Stock Listing (
_StockListPageState
): This class manages the state of the stock listing page. It initializes an empty list of stocks and makes an HTTP request to a public API in theinitState()
method. -
Stock Fetching Method (
fetchStock
): This method uses thehttp
package to make a GET request to the stocks API URL. If the response is successful (status code 200), the JSON data is decoded, and the list of stocks is updated in the state. Otherwise, an exception is thrown. -
UI Construction (
build
): Thebuild
method constructs the UI of the stock listing page. It displays anAppBar
with the title "Stocks" and aListView.builder
that displays each stock as aListTile
. -
Stock Listing (
ListView.builder
): This is a scrollable list of stocks, where each stock is displayed as aListTile
containing the stock symbol, sector, and latest price.
- Clone or download this repository.
- Ensure you have Flutter installed and set up in your development environment.
- Run the app on a device or emulator.
- Flutter SDK
- Internet connection to fetch stock data
This app uses a public API to fetch stock data. Make sure to adhere to the API's terms of use and consider potential usage limitations.