Kinda Code
Home/Flutter/Flutter: Call a void function from another file

Flutter: Call a void function from another file

Last updated: February 15, 2023

To call a void function from another file in Flutter, there are 2 points you need to know:

  • DO NOT name the function with the underscore symbol ( _ ) at the beginning. For instance, _myFunction() cannot be called in another file.
  • Import the file that contains the function.

Example

Create a file called test.dart in the lib folder in your Flutter project:

void doSomething(){
  print('Hello man');
}

Call the doSomething function in main.dart:

// import test.dart
import './test.dart';

// Other code

// Use the doSomething function
TextButton(child: const Text('Button'), onPressed: doSomething,)

Hope this helps!

Further reading:

You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.