0

I have a bool fetchNew. It's initialized as false and I have a function refresh() that sets it to true. How do I set it to false right after setState preventing the rerender?

class _DashboardState extends State<Dashboard> {
  bool fetchNew = false;

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

  refresh() {
    setState(() {
      fetchNew = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

I thought initState() would set it to false again after rerender but the way I understand initState is that it will only fire once to inject the Widget into the widget tree.

  • 1
    You want to set it true for 1 second only and change it to false again? – najdat Akkad Aug 22 '21 at 14:36
  • Yes basically. setState rerenders the widget. I just want it to be false right after setting it true so it won't rerender twice. I couldn't find any flutter lifecycle that would achieve that. Something like change state after rerender without rerending again. – Lukas Luke Stateczny Aug 22 '21 at 14:39
  • Just add `fetchNew = false` after the `setState()` in your `refresh()` function. – stacktrace2234 Aug 22 '21 at 15:16

1 Answers1

3

can you try this?

setState(() {
  fetchNew = true;
  Future.delayed(Duration(milliseconds: 100, () {
     fetchNew = false;
  });
});
najdat Akkad
  • 195
  • 8