How to use Functions of Another File in Flutter?
Last Updated :
09 Mar, 2022
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 going to learn How to use the Function of another file in Dart or Flutter.
There are a couple of different methods for implementing the Function of another file in dart or flutter and some are Global function, static method, mixin, etc.
Method 1: Global function
Using global functions doesn't follow Object Oriented programming concept. Global state breaks the Encapsulation principle. OOP is the best basic paradigm to use within Dart apps because of the way the language is designed (classes).
Define a function in a file, just say global.dart.
Dart
void func()
{
print('Hello GEEKS FOR GEEKS');
}
To use it in any file, just call:
Dart
import 'package:sample/global.dart';
main() {
func();
}
Note: Don't forget to import the global.dart file.
Method 2: Static function in a Class
Static functions break the encapsulation property of OOP and the Open-Closed principle. They are hard to integrate with state management solutions because we track the state within instance method contexts. For example, integrating providers and other packages like GetX.
Create a class, say Sample in file global.dart , and define your function func in it:
Dart
class Sample {
static void func() => print('Hello Geeks for Geeks');
}
To use it in any file, just call:
Dart
Note: Don't forget to import the global.dart file.
Method 3: Mixins
Dart has inbuilt support for optionally adding functions to a class when we want to reduce duplicated code but avoid extending the whole class (Source). The mixin keyword enables this by mixing a class with some specific logic. We can restrict the mixin to a specific subclass with on if needed.
Create a mixin, say web:
Dart
mixin Web {
void func() => print('Hello');
}
To use it in a class, just use with a keyword followed by the mixin.
Dart
class Site with Web {
void main() => func();
}
These are a couple of ways you can use a function from another file in Dart/Flutter.
Similar Reads
How to Import Data From One Page to Another in Flutter? Flutter is Googleâs Mobile SDK to build native iOS and Android, Desktop (Windows, Linux, macOS), Web apps from a single codebase. When building applications with Flutter everything towards Widgets â the blocks with which the flutter apps are built. They are structural elements that ship with a bunch
5 min read
Flutter - Pick and Open Files From Storage Sometimes in an app, we need to pick and open a file from the phone's storage, in this article, we will achieve the same using file_picker and open_file flutter packages. 1. Create an App: Create a new flutter app by running the below command on your terminal: flutter create your_app_name Now open y
5 min read
Flutter - Pass a Function as an Argument From One Screen to Another In Flutter, you can pass a function as an argument from one screen (widget) to another screen (widget) by defining a callback function and passing it as an argument when navigating or pushing to the new screen. Here's a step-by-step guide on how to do this: Required Tools To build this app, you need
4 min read
Flutter - Working with Callback Functions 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 simples
3 min read
Flutter - Navigate From One Screen to Another Flutter apps may have multiple screens or pages. Pages are groups of functionalities. The user navigates between different pages to use different functionalities. Concepts like pages are called routes in Flutter. We can use Navigator.push() to navigate to a new route and Navigator.pop() to navigate
3 min read
How to Add Firebase into Flutter in a New Way? Recently Firebase give another option for adding Firebase to Flutter and It is only made for Flutter, As of now we add firebase using the android option for flutter but now we can add using the flutter option that is recently updated by Flutter. Here you can find how to add firebase as an Android in
2 min read
How to Build Advance Quiz App in Flutter? This is a quiz app using Flutter and API. The working of this application is very simple, Â here we have two screens: screen one and screen two. Screen one is the stateless screen which means that it will not respond to any change on the screen. The second screen, is a stateful widget which means tha
9 min read
Flutter - Pass Data One Screen To Another Screen In this article we gonna know how you can pass the data from one screen to another screen in Flutter. In this example we took two screens one and screen two, In the first screen, you enter the data, and that data you can see on your second screen. This is the main object of this application. If we t
4 min read
Flutter - How to Use SVG Image SVG (Scalable Vector Graphics) is a vector image format that can be used to create high-quality, resolution-independent graphics. In Flutter, you can use SVG images to display vector graphics in your app. To use SVG images in Flutter, you can use the  flutter_svg package to display SVG images in Flu
3 min read
How to Save the File in Phone Storage in Flutter? Like in Instagram, we can save stories, and reels on our devices. We also want to save some information or some file on our device in android we will keep the file from the flutter app on android devices. Step By Step Implementation Step 1: Create a New Project in Android Studio To set up Flutter De
3 min read