Reputation: 171
When I make a get request, it sends endless requests, I couldn't understand where the error is
When I make a get request, it sends endless requests, I couldn't understand where the error is
String url =
'https://api.coingecko.com/api/v3/coins/markets?
vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=false';
KriptoModel? kriptoModel;
Future? kriptoGet;
Future kriptoGetir() async {
final response = await http.get(Uri.parse(url));
final durum = response.statusCode;
print(durum.toString());
if (durum == 200) {
var jsonRes = jsonDecode(response.body);
List<KriptoModel> kriptoList = [];
for (var i = 0; i < jsonRes.length; i++) {
kriptoList.add(KriptoModel.fromJson(jsonRes[i]));
}
setState(() {
kriptoList;
});
;
return kriptoList;
} else {
return throw Exception('istek başarısız');
}
}
I call cryptoGet method every minute in init state
@override
void initState() {
super.initState();
kriptoGet = kriptoGetir();
Timer.periodic(
Duration(second: 60), ((timer) => kriptoGet =
kriptoGetir()));
}
Future builder print the data to the screen
FutureBuilder(
future: kriptoGetir(),
builder: (context, veri) {
if (veri.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
} else {
return Container(
width: MediaQuery.of(context).size.width,
height: 70,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: const Color.fromARGB(255, 13, 2, 31)),
child: ListTile(
leading:
Image.network(veri.data![index].image, width: 35),
title: Text(
veri.data![index].id.toString(),
style: TextStyle(color: Colors.white),
),
subtitle: Text(
'\$ ${veri.data![index].currentPrice}',
style: TextStyle(color: Colors.white, fontSize: 14),
),
trailing: methodDegisimKontrol(
veri.data?[index].marketCapChangePercentage24h),
onTap: () {
/* Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Deneme(),
));*/
},
),
);
}
});
this is my model class
class KriptoModel {
final dynamic id;
final dynamic symbol;
final dynamic image;
final dynamic currentPrice;
final dynamic marketCap;
final dynamic marketCapRank;
final dynamic fullyDilutedValuation;
final dynamic totalVolume;
final dynamic high24h;
final dynamic low24h;
final dynamic priceChange24h;
final dynamic priceChangePercentage24h;
final dynamic marketCapChange24h;
final dynamic marketCapChangePercentage24h;
final dynamic circulatingSupply;
final dynamic totalSupply;
final dynamic maxSupply;
final dynamic ath;
final dynamic athChangePercentage;
final dynamic athDate;
final dynamic atl;
final dynamic atlChangePercentage;
final dynamic atlDate;
final dynamic roi;
final dynamic lastUpdated;
KriptoModel({
this.marketCapRank,
this.fullyDilutedValuation,
this.high24h,
this.low24h,
this.priceChange24h,
this.priceChangePercentage24h,
this.marketCapChange24h,
this.marketCapChangePercentage24h,
this.circulatingSupply,
this.totalSupply,
this.maxSupply,
this.ath,
this.athChangePercentage,
this.athDate,
this.atl,
this.atlChangePercentage,
this.atlDate,
this.lastUpdated,
this.id,
this.symbol,
this.image,
this.currentPrice,
this.marketCap,
this.totalVolume,
this.roi,
});
factory KriptoModel.fromJson(Map<String, dynamic> json) {
return KriptoModel(
id: json['name'],
symbol: json['symbol'],
image: json['image'],
currentPrice: json['current_price'],
marketCap: json['market_cap'],
marketCapRank: json['market_cap_rank'],
fullyDilutedValuation: json["fully_diluted_valuation"],
totalVolume: json["total_volume"],
high24h: json["high_24h"],
low24h: json["low_24h"],
priceChange24h: json["price_change_24h"],
priceChangePercentage24h: json["price_change_percentage_24h"],
marketCapChange24h: json["market_cap_change_24h"],
marketCapChangePercentage24h: json["market_cap_change_percentage_24h"],
circulatingSupply: json["circulating_supply"],
totalSupply: json["total_supply"],
maxSupply: json["max_supply"],
ath: json["ath"],
athChangePercentage: json["ath_change_percentage"],
athDate: json["ath_date"],
atl: json["atl"],
atlChangePercentage: json["atl_change_percentage"],
atlDate: json["atl_date"],
roi: json["roi"],
lastUpdated: json["last_updated"],
);
}
}
Upvotes: 1
Views: 74
Reputation: 23357
The problem is that you call the function that does the request inside the FutureBuilder
. This will make it call it again after each rebuild, which is after each setState, which is every time the request is complete. Making it go on forever. The solution is to do this request in the init state and put a reference to the future in the Future builder. That reference you actually already made as kriptoGet
So change
FutureBuilder(
future: kriptoGetir(),
to
FutureBuilder(
future: kriptoGet,
Upvotes: 1