Reputation: 1
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
Reputation: 120
You can call the function before returning you Page enter image description here
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
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