Home > Enterprise >  How can I manage page start with getx?
How can I manage page start with getx?

Time:11-25

When I use the init state, every time I enter the page, it sends a request to the api again, but when the first application starts and I want it to send the request at the time I want, how can I do this. thanks

Future<List<KriptoModel>> kriptoGetir() async {

    kriptoList = [];
    final response = await http.get(Uri.parse(url));

  if (response.statusCode == 200) {
     List<dynamic> jsonRes = [];
     jsonRes = json.decode(response.body);

     for (var i = 0; i < jsonRes.length; i  ) {
         if (jsonRes[i] != null) {
         Map<String, dynamic> map = jsonRes[i];
         kriptoList.add(KriptoModel.fromJson(map));
         list.add(jsonRes[i]['sparkline_in_7d']['price']);
      }
    }
     if (this.mounted) {
       setState(() {
         list;
        kriptoList;
      });
    }
     print('istek gönderildi coin');
     return kriptoList;
  } else {
     return throw Exception('istek başarısız');
 }
}



 Future<List> haberGetir() async {
   final Xml2Json xml2json = Xml2Json();
   var res = await http.get(Uri.parse(haberUrl));

   if (res.statusCode == 200) {
      var jsonRes;
      xml2json.parse(res.body);
      var jsonString = await xml2json.toParker();
      jsonRes = jsonDecode(jsonString);
      var jsonLs = jsonRes['haberler']['haber'] as List;
        haberList = (jsonLs as List).map((e) => 
        Haber.fromJson(e)).toList();
     if (this.mounted) {
       setState(() {
        haberList;
       });
     }
       print('haber gönderildi');
      return haberList;
    } else {
    return throw Exception();
  }
 }

I want the methods here to work with the time difference I want.

 @override
 void initState() {
    haberGetir();
   kriptoGetir();

    Timer.periodic(Duration(seconds: 260), ((timer) => 
    haberGetir()));
    Timer.periodic(Duration(seconds: 45), ((timer) => 
    kriptoGetir()));

 super.initState();
}

CodePudding user response:

As far as I understand, you don't turn off the timers when the page is closed. enter image description here

CodePudding user response:

 @override
 void initState() {

  Timer.periodic(Duration(seconds: 55), ((timer) => 
  haberGetir()));
  Timer.periodic(Duration(seconds: 55), ((timer) => 
  kriptoGetir()));
  super.initState();
}
This way, the problem is solved, but the data comes 
late when the application is first opened.
  • Related