Skip to content

Instantly share code, notes, and snippets.

@Bilguun132
Created March 15, 2019 16:02
Show Gist options
  • Select an option

  • Save Bilguun132/88789f736ce01d32ba62894f226189c0 to your computer and use it in GitHub Desktop.

Select an option

Save Bilguun132/88789f736ce01d32ba62894f226189c0 to your computer and use it in GitHub Desktop.
crypto_list-FlutterDemo-main-3
import 'package:flutter/material.dart';
import 'dart:math';
import 'dart:async';
import 'dart:convert';
import 'dart:core';
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//material app widget
return MaterialApp(
title: 'Crypto Price List',
theme: new ThemeData(primaryColor: Colors.white),
home: CryptoList(),
); //use our widget instead of the text previously
}
}
//creates a stateful widget (data inside will change once created)
class CryptoList extends StatefulWidget {
@override
CryptoListState createState() => CryptoListState();
}
class CryptoListState extends State<CryptoList> {
List _cryptoList;
//this means that the function will be executed sometime in the future (in this case does not return data)
Future<void> getCryptoPrices() async {
//async to use await, which suspends the current function, while it does other stuff and resumes when data ready
print('getting crypto prices'); //print
String _apiURL =
"https://api.coinmarketcap.com/v1/ticker/"; //url to get data
http.Response response = await http.get(_apiURL); //waits for response
setState(() {
this._cryptoList =
jsonDecode(response.body); //sets the state of our widget
print(_cryptoList); //prints the list
});
return;
}
@override
void initState() { //override creation of state so that we can call our function
super.initState();
getCryptoPrices(); //this function is called which then sets the state of our app
}
//build method
@override
Widget build(BuildContext context) {
//Implements the basic Material Design visual layout structure.
//This class provides APIs for showing drawers, snack bars, and bottom sheets.
return Scaffold(
appBar: AppBar(
title: Text('CryptoList'),
actions: <Widget>[
// will be used to view favourites
new IconButton(icon: const Icon(Icons.list), onPressed: _pushSaved),
],
),
body: new Center(
// body of the scaffold
child: new Text('my crypto app'),
));
}
//will be used later to view favourited cryptos
void _pushSaved() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment