yusufmustu
yusufmustu

Reputation: 1

Function that runs on the opening of all Pages

How can I write a function in flutter that works before opening it for all pages? For example, before I open all the pages I have created, I want to check if there is internet, free space on the device, charge status of the device is higher than x, etc.

Upvotes: 0

Views: 42

Answers (2)

Dhruv Vanjari
Dhruv Vanjari

Reputation: 120

For a Stateless Page (widget)

You can call the function before returning you Page enter image description here

For a Stateful Page (widget)

You can call the function in the initState() method. It will call this function exactly once before your page gets built. (i.e. the build method gets called) For Stateful widgets

I hope this is helpful.

Upvotes: 0

Tanuj
Tanuj

Reputation: 2260

You can achieve this using a stateful widget, by overriding initState() method of a stateful widget, something like this -

class Example extends StatefulWidget {
  _ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
  @override
  void initState() {
    //Do your initialization stuff here
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Hope this helps!

Upvotes: 1

Related Questions