Bhavik Dalal
Bhavik Dalal

Reputation: 115

How to call a function from another page in flutter?

So I have a call a timer function on pressing the login button in the main file. The timer function is defined in the login verification file. So how can I call the timer function in main file so that when the login button is pressed and when it goes to the login verification the timer starts?

Upvotes: 0

Views: 1441

Answers (1)

dzenan9999
dzenan9999

Reputation: 43

If you want to use some parent's function in child widget then it is easy - just pass the reference to the function you want through the constructor.

If you want to use some child's function in parent widget then the situation is a little bit different - you have to pass a GlobalKey to identify the state of child widget when creating an instance of this widget. You can refer to this https://stacksecrets.com/flutter/how-to-call-method-of-a-child-widget-from-parent-in-flutter.

If you want to use a function from a widget that is not either a child or parent widget, then you might think of using some state managment solution (such as Provider or GetX or Bloc) to provide it to all the widget that needs some information from it.

Upvotes: 1

Related Questions