Niek Biesterbos
Niek Biesterbos

Reputation: 175

How to prevent disposing a screen when using BottomNavigationBar in Flutter?

I have a simple async function and I call this function in my initState. This function reads my Firebase Firestore database and I want to keep the amount of reads as low as possible. But when using a BottomNavigationBar, and navigating with body: _widgetOptions.elementAt(index), a screen gets disposed whenever is navigated to a new screen. When I return back to the original screen initState is called again and my data is being loaded again while this is not something I need or want.

Does anyone have a solution or tips?

Thanks in advance!

Upvotes: 2

Views: 744

Answers (1)

Akif
Akif

Reputation: 7650

You can set a global boolean variable in your provider. Let say, isCalled is with the default value false. Then, you can check if isCalled is false before calling your method. And then, you can set it true after calling your method.

Upvotes: 1

Related Questions