Flutter - Working with Callback Functions Last Updated : 21 Jun, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will see how we can use callback functions in flutter. We will learn about different methods to implement callback functions in flutter. Callback is basically a function or a method that we pass as an argument into another function or a method to perform an action. In the simplest words, we can say that Callback or VoidCallback are used while sending data from one method to another and vice-versa. It is very important to maintain a continuous flow of data throughout the flutter app. Let's assume that you are working on an app. This app displays some sort of data. Now to alter the values in the application, there are 2 approaches that you can take, either change the state using various state-altering techniques or change the value using a Callback. If we are to work with the Callback function there are 2 possible methods that we can use as shown below: Method 1: Directly writing the callback In this approach, we just define the function that is supposed to trigger a callback when a specific event occurs. Example: Dart import 'package:flutter/material.dart'; // function to trigger the app build process void main() { runApp(MaterialApp( home: Scaffold( // appbar appBar: AppBar( title: const Text('GeeksForGeeks'), backgroundColor: const Color.fromRGBO(15, 157, 88, 1), ), body: const MyApp(), ), )); } class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @override // ignore: library_private_types_in_public_api _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { int count = 0; @override Widget build(BuildContext context) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( '$count', style: const TextStyle(fontSize: 50.0), ), ElevatedButton( style: ButtonStyle( backgroundColor: MaterialStateProperty.all(Colors.green)), onPressed: () { setState(() { count++; }); }, child: const Text('' 'increase'), ), // RaisedButton is deprecated and shouldn't be used. // Use ElevatedButton insetad. // RaisedButton( // // callback function // // this increments the value // // by 1 each time the Raised button is pressed // onPressed: () { // setState(() { // count++; // }); // }, // child: const Text('' // 'increase'), // ) ], ), ); } } Output: Method 2: Passing the callback function In this approach, the callback is directly passed to the event. As shown in the below example, the onPressed action makes the direct callback function defined in the earlier part of the code. Example: Dart import 'package:flutter/material.dart'; void main() { runApp(MaterialApp( home: Scaffold( // appbar appBar: AppBar( title: const Text('GeeksForGeeks'), backgroundColor: const Color.fromRGBO(15, 157, 88, 1), ), body: const MyApp(), ), )); } class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @override // ignore: library_private_types_in_public_api _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { int count = 0; // callback function callBack() { setState(() { count++; }); } @override Widget build(BuildContext context) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( '$count', style: const TextStyle(fontSize: 50.0), ), ElevatedButton( style: ButtonStyle( backgroundColor: MaterialStateProperty.all(Colors.green)), onPressed: callBack, child: const Text('increase'), ), // RaisedButton is deprecated and shouldn't be used. // Use ElevatedButton insetad. // RaisedButton( // // callback on Button press // onPressed: callBack, // child: Text('' // 'increase'), // ) ], ), ); } } Output: Comment More infoAdvertise with us Next Article Flutter - Working with Callback Functions chetankhanna767 Follow Improve Article Tags : Dart Flutter Android Flutter Similar Reads Flutter - Working with Layouts Before talking about Layout in Flutter, there is just one thing to keep in mind that âEverything in Flutter is Widget". Meaning the core of the layout in any Flutter Application is the widget. Putting it simply, all the images, icons, labels and text, etc are technically widgets of different types a 6 min read Flutter - Working with Animations Whenever building an app animation plays a vital role in designing the experience of the user. People tend to like an app that has a smooth flow and a slick design. The Flutter Package provides a variety of methods to create and use animation in our app. We will be discussing the inbuilt Flutter wid 9 min read Making Calls in Flutter In this online world, customer care plays a major role in the success of a company. Users are quite satisfied when they converse with the executives through calls. This has forced companies to add phone numbers to their apps for their customers to contact them easily. But dialing those numbers from 7 min read How to use Functions of Another File in Flutter? Flutter is an open-source framework by Google for building beautiful, natively compiled, multi-platform applications from a single codebase. A Single codebase means that only we have to write a single code for Android and IOS and others like Desktop  Application, Web Application. So, Today we are go 2 min read What are Widgets Available in Flutter? Flutter is a mobile app development framework that allows developers to create beautiful and responsive user interfaces with ease. This is made possible through the use of Flutter widgets, which are the building blocks of every Flutter application. In Flutter, widgets are the fundamental building bl 4 min read Motion Toast Widget in Flutter A Motion Toast widget in Flutter is a type of toast message that animates its appearance on the screen. It can be used to provide feedback or notifications to the user in a subtle, yet attention-grabbing way. One way to implement a Motion Toast in Flutter is to use the AnimatedContainer widget. You 3 min read Flutter - Splitting App into Widgets Splitting an app into widgets refers to the breaking up of large widgets into smaller, more manageable widgets which can be inferred as smaller chunks of codes. The principal thing here is to understand when to split a widget into smaller ones. We are going to discuss when to split a widget and meth 5 min read What is Widgets in Flutter? Flutter is Google's UI toolkit for crafting beautiful, natively compiled iOS and Android apps from a single code base. To build any application we start with widgets - The building block of Flutter applications. Widgets describe what their view should look like given their current configuration and 5 min read FlashCards Learning App in Flutter The FlashCard learning app is the one-stop destination to create an interactive learning/quiz app. This app was developed using Flutter. Flutter is the tool used to build cross-platform applications for different platforms and OS. In this article, we will learn to develop a flashcard learning app. Y 7 min read Running Background Tasks in Flutter Creating an application that is able to handle processes with background capabilities is crucial for numerous applications, including geolocation, music, and other processes that may take a lot of time. Flutter, an open-source UI toolkit owned by Google, lets developers write and compile application 6 min read Like