“kesalahan nol karena keterlambatan respons API” Kode Jawaban

kesalahan nol karena keterlambatan respons API

String text;

fetchData() async {
//...
  text = weatherData.weather[0].main ?? 'Waiting api response...';
//...
}

// in your build method
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Container(
      child: Text(text), //this will render "Waiting api response" first, and when the api result arrive, it will change
    ),
  );
}

Thankful Tapir

kesalahan nol karena keterlambatan respons API

Future fetchDataCity() async {
  // your code
  weatherCity = WeatherCity.fromJson(cityDecodedJson);
  return weatherCity;
}

Future fetchDataWeather() async {
  // your code
  weatherData = WeatherData.fromJson(decodedJson);
  return weatherData;
}

// in your build method
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Container(
      child: FutureBuilder(
        future: fetchDataWeather(), // a previously-obtained Future or null
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          switch (snapshot.connectionState)
            case ConnectionState.active:
            case ConnectionState.waiting:
              return Text('Awaiting result...'); //or a placeholder
            case ConnectionState.done:
              if (snapshot.hasError){
                return Text('Error: ${snapshot.error}');
              } else {
                return Text('Error: ${snapshot.data}');
            }
         },
      ) //FutureBuilder
    ),
  );
}
Thankful Tapir

Jawaban yang mirip dengan “kesalahan nol karena keterlambatan respons API”

Pertanyaan yang mirip dengan “kesalahan nol karena keterlambatan respons API”

Lebih banyak jawaban terkait untuk “kesalahan nol karena keterlambatan respons API” di Dart

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya