I have landing Flutter page (main.dart). I have JSON data and I am using Future to get it. My question is that if the data is null or if the JSON link is dead how can I show the app with empty data.
My problem is my Flutter app Strats with a white screen then it turns black with showing CircularProgressIndicator. If the above error exists its keeps running a black screen with CircularProgressIndicator.
When my App starts from the second I need to show the CircularProgressIndicator and do the rest. And if the JSON data is null or link is dead I still need to show my app with empty data and show some warning.
// TODO: 4) _MyHomePageState Class
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
// TODO: implement initState
super.initState();
this.getCurrencyJsonData();
}
Future<String> getCurrencyJsonData() async {
var response = await http.get(
Uri.encodeFull("https://secure.*****************fx.jsp"),
headers: {'Accept': 'application/json'});
setState(() {
var resBody = json.decode(response.body);
currencyData = resBody["currency"];
stgBuy = currencyData["sterling"]["buy"];
print("STG: $stgBuy");
});
return "Success!";
}
// TODO: BUILD WIDGET
@override
Widget build(BuildContext context) {
if (currencyData == null){
return new Center(
child: new CircularProgressIndicator(
backgroundColor: lightMainGreen,
)
);
} else {
return new Scaffold(
// APPBAR
appBar: new AppBar(
title: new Text(
……
……
……