Flutter - Future.delayed with Example
Last Updated :
24 Apr, 2025
You can use the Future.delayed method in Flutter to delay the execution of a piece of code for a specified duration. A sample video is given below to get an idea about what we are going to do in this article.
How to Use?
Dart
Future.delayed(Duration(seconds: 2), () {
// code to be executed after 2 seconds
});
Duration(seconds: 2) specifies the duration to delay the execution by 2 seconds. The second argument to the Future.delayed method is a callback function that contains the code to be executed after the delay.
Step By Step Implementation
Step 1: Create a New Project in Android Studio or in vs code.
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Add the material package that gives us the important methods and then call the runApp method in the main function that will call our application.
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
Step 3: Now we have to make a stateless widget RunMyApp Because our application does not go to change its state and then return the MaterialApp widget which allows us the set the title and theme and many more. Further in the home property give Delayedwidget (Not defined yet).
class RunMyApp extends StatelessWidget {
const RunMyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Delayed Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: DelayedWidget);
}
}
Step 4: Let's create A DelayedWidget class and this going to be a stateful widget
In this class, we defined the bool variable _showText which is used whether the text is shown or not. Also, we create the Button in the center of the body, so that we can setState method. Inside the setState method, we use Future delay to delay for some time. Then we can simply display the text.
class DelayedWidget extends StatefulWidget {
const DelayedWidget({super.key});
@override
_DelayedWidgetState createState() => _DelayedWidgetState();
}
class _DelayedWidgetState extends State<DelayedWidget> {
bool _showText = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Delayed Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_showText)
Text(
'Delayed Text GeeksforGeeks',
style: TextStyle(fontSize: 24),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
setState(() {
_showText = false;
});
Future.delayed(Duration(seconds: 3), () {
setState(() {
_showText = true;
});
});
},
child: Text('Show Text After Delay 3 seconds'),
),
],
),
),
);
}
}
We use the setState method to change the state of the _showText variable, We first make it false and then Use the Future delay of the 3 seconds. Further makes _showText to true.
Code Example
Dart
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatelessWidget {
const RunMyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Delayed Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: DelayedWidget(),
);
}
}
class DelayedWidget extends StatefulWidget {
const DelayedWidget({super.key});
@override
_DelayedWidgetState createState() => _DelayedWidgetState();
}
class _DelayedWidgetState extends State<DelayedWidget> {
bool _showText = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Delayed Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_showText)
Text(
'Delayed Text GeeksforGeeks',
style: TextStyle(fontSize: 24),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
setState(() {
_showText = false;
});
Future.delayed(Duration(seconds: 3), () {
setState(() {
_showText = true;
});
});
},
child: Text('Show Text After Delay 3 seconds'),
),
],
),
),
);
}
}
Output
Similar Reads
Flutter - dispose() Method with Example
Dispose is a method triggered whenever the created object from the stateful widget is removed permanently from the widget tree. It is generally overridden and called only when the state object is destroyed. Dispose releases the memory allocated to the existing variables of the state. It is only used
3 min read
Flutter - Loading Kit Widget with Example
In every android application, you have seen the loading bars, which are used when something is loading such as loading data, loading the screen, and fetching the data. The same flutter gives you a widget Flutter loading kit. We simply use the widget and use it in our project. A sample video is given
2 min read
Flutter - Fade a Widget In and Out
The AnimatedOpacity widget in Flutter allows you to animate the opacity (transparency) of a child widget. You can use it to smoothly fade in or out a widget. In this article, we are going to implement the AnimatedOpacity Widget in Flutter and see the basic syntax of it. A sample video is given below
4 min read
Flutter - What is Future and How to Use It?
Future in Flutter refers to an object that represents a value that is not yet available but will be at some point in the future. A Future can be used to represent an asynchronous operation that is being performed, such as fetching data from a web API, reading from a file, or performing a computation
3 min read
Flutter - Animated AlertDialog in Flutter
Animating an AlertDialog in Flutter involves using the Flutter animations framework to create custom animations for showing and hiding the dialogue. In this article, we are going to add an animation to an AlertDialog. A sample video is given below to get an idea about what we are going to do in this
4 min read
Draggable Widget in Flutter
In Flutter, a Draggable widget can be used to allow users to interact with a widget by dragging it around the screen. To create a Draggable widget, you can use the Draggable class and pass it to a child widget to be rendered, along with a feedback widget that will be displayed while the user is drag
4 min read
Flutter - FutureBuilder Widget
In Flutter, the FutureBuilder Widget is used to create widgets based on the latest snapshot of interaction with a Future. Â It is necessary for Future to be obtained earlier either through a change of state or change in dependencies. FutureBuilder is a Widget that will help you to execute some asynch
4 min read
Flutter - Read and Write Data on Firebase
Firebase helps developers to manage their mobile apps easily. It is a service provided by Google. Firebase has various functionalities available to help developers manage and grow their mobile apps. In this article, we will learn how to write and read data into/from Firebase. Follow the 3-step proce
4 min read
Flutter - Generate Dynamic Widgets
Dynamic widgets are a powerful feature in Flutter that allow you to generate widgets at runtime based on data or user input. In this article, we'll focus on generating dynamic text fields in Flutter. Use Cases Dynamic widgets in Flutter are extremely useful in situations where you need to create a v
5 min read
Flutter - Create Fortune Wheel Spin
Deciding what to eat can be a daily dilemma, especially when you have a plethora of options to choose from. To make this decision-making process more exciting and interactive, in this article, we will be creating â the Lunch Spinning Wheel app.Steps to Implement Fortune Wheel in FlutterStep 1: Creat
6 min read