null error due to delay in api response code example
Example 1: null error due to delay in api response
Future fetchDataCity() async {
weatherCity = WeatherCity.fromJson(cityDecodedJson);
return weatherCity;
}
Future fetchDataWeather() async {
weatherData = WeatherData.fromJson(decodedJson);
return weatherData;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: FutureBuilder(
future: fetchDataWeather(),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState)
case ConnectionState.active:
case ConnectionState.waiting:
return Text('Awaiting result...');
case ConnectionState.done:
if (snapshot.hasError){
return Text('Error: ${snapshot.error}');
} else {
return Text('Error: ${snapshot.data}');
}
},
)
),
);
}
Example 2: null error due to delay in api response
String text;
fetchData() async {
text = weatherData.weather[0].main ?? 'Waiting api response...';
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Text(text),
),
);
}