'Listen to real-time change in Flutter from node.js api (mysql database)

I created my own api for (get/post/put/delete) methods using node js.....and implemented in flutter....how can i get real time change (change state without refresh the application) if i post/edit/delete request in flutter....

Stream<List> fetchData() async* { List product = [];

var url = Uri.parse(Url.get);
var response = await http.get(url);
if (response.statusCode == 200) {
  var decodedData = jsonDecode(response.body);
  decodedData.forEach((element) {
    product.add(Product.fromJson(element));
  });
}

yield product;

}

after this i call this stream to streambuilder()



Solution 1:[1]

Poll API and post results to a stream, something like this

  Stream<String> datetime() {
    return Stream.periodic(
      const Duration(seconds: 10), 
      (_) => http.get(Uri.parse('https://worldtimeapi.org/api/ip'))
    ).asyncMap((r) async => await r)
      .map((r) => json.decode(r.body))
      .map((json) => json['datetime']);
  }

Call API once and post result to a stream

  Stream<String> datetime() {
    return Stream.fromFuture(
      http.get(Uri.parse('https://worldtimeapi.org/api/ip'))
    ).map((r) => json.decode(r.body))
     .map((json) => json['datetime']);
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1