Flutter: Call a void function from another file

Updated: February 15, 2023 By: Augustus 2 comments

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.

Subscribe
Notify of
guest
2 Comments
Inline Feedbacks
View all comments
Linuse
Linuse
2 years ago

The 1) help me. Thank.

A Goodman
Admin
A Goodman
2 years ago
Reply to  Linuse

I’m glad to hear that 🙂

Related Articles