Home > Net >  How to update list values on initstate in flutter?
How to update list values on initstate in flutter?

Time:08-18

I want to update the newList when the state gets initiated and want to store api response into that list.

class _DashboardScreenState extends State<DashboardScreen> {
List newList = [];
  getSlideBanners() async {

    var resp = await http.get(Uri.parse("xxx"));

    newList.addAll(jsonDecode(resp.body));

    return jsonDecode(resp.body);
  }

}

CodePudding user response:

Future method will take some time to fetch, you can do it like

@override
void initState() {
  super.initState();

  getSlideBanners().then(
    (value) {
      //here 
      setState(() {});
    },
  );
}

CodePudding user response:

You can do thsi way also :

@override
void initState() {
  getSlideBanners();
  
  super.initState();
}

getSlideBanners() async {

    var resp = await http.get(Uri.parse("xxx"));

    newList.addAll(jsonDecode(resp.body));
     //here you can do setState 
    setState(() {});
  }
  • Related