Created
May 31, 2022 20:24
-
-
Save pxpc2/6be129a40afff41a6245f24d8a2b9ec0 to your computer and use it in GitHub Desktop.
btc value fetcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:ffi'; | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
class Home extends StatefulWidget { | |
const Home({Key? key}) : super(key: key); | |
@override | |
State<Home> createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
late String btcResponse; | |
Future<Map> makeCall() async { | |
const String url = "https://blockchain.info/ticker"; | |
http.Response response = await http.get(Uri.parse(url)); | |
return json.decode(response.body); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return FutureBuilder<Map>(builder: (context, snapshot) { | |
Widget futureChild; | |
if (snapshot.hasData) { | |
double value = snapshot.data!["BRL"]["buy"]; | |
futureChild = Text("BTC value in BRL is $value"); | |
} else if (snapshot.hasError) { | |
futureChild = Text("Error has occured: ${snapshot.error}"); | |
} else { | |
// waiting for data still | |
print("waiting data"); | |
futureChild = const CircularProgressIndicator(); | |
} | |
return Center(child: Scaffold( | |
appBar: AppBar(title: Text("App"), backgroundColor: Colors.deepPurple,), | |
body: Center( | |
child: Padding( | |
child: SingleChildScrollView(child: futureChild), | |
padding: EdgeInsets.only(top: 60, bottom: 60), | |
) | |
), | |
)); | |
}, future: makeCall()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment