0
votes

if changes in my api then how to use setstate in my flutter app Generally I used to updata my screen to back then come at that screen plz any body give answer to that question plz

1

1 Answers

0
votes

To render the UI based on the response you can use a stream builder.. This builder rebuilds the UI when there is a change in the response


Stream<ResponseModel> responseStream() async {
  while (true) {
    await Future.delayed(Duration(seconds: 1));
    ResponseModel responseObj = getResponseFromAPI();
    yield responseObj;
  }
}


StreamBuilder(
          stream: responseStream(),
          builder: (context, AsyncSnapshot<String> snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const CircularProgressIndicator();
            }
            return Text(
              snapshot.data!,
            );
          },
        ),
      ),