How to Call Recurring Function in Flutter Dart Repeat Method

In a programming language Recurring function means a function which calls again and again automatically after a particular time without disturbance. In flutter we can make our own recurring method using Timer.periodic() method. The Timer.periodic() methods enable us to call a function or statement after a particular given time again and again. The margin of time is the delay between each calling of statement. So in this tutorial we would learn about Example of  Call Recurring Function in Flutter Dart Repeat Method in Android iOS.

Contents in this project Example of  Call Recurring Function in Flutter Dart Repeat Method Android iOS:

1. Open your project’s main.dart file and import material.dart and dart:async package. The material package contains all the widgets in flutter and dart:async package contains the Timer class.

2. Creating void main runApp() method and here we would call our main MyApp class.

3. Creating our main Root class named as MyApp extends StatelessWidget class. In this class we would call our App class.

4. Creating another class named as App. In the App class we would create our main State class named as AppState with createState() method to enable mutable State integration in given class.

5. Creating our main AppState extends State<App> class. In this class we would do all the main coding.

6. Creating the object of Timer class named as _timer and also we would make a integer type variable named as counter with default value of Zero.

7. Creating a function named as updateCounter(). Inside the function we would call the Timer.periodic() method with time duration of 1 Second. We would update the counter State value to +1 on after every second of calling.

8. Creating another function named as stopCounter(). We would use this function to STOP the timer on a particular event as per user choice. So when use calls this function it’ll stop the timer.

9. Creating Widget Build Area -> Scaffold Widget -> Center Widget -> column widget -> Text widget and 2 Raised Button widgets. We would display the counter variable value on Text widget.

10. Complete source code for main.dart file:

Screenshots:

How to Call Recurring Function in Flutter Dart Repeat Method

Leave a Reply

Your email address will not be published. Required fields are marked *