6 C
New York
Monday, December 11, 2023

Flutter: Call a void function from another file

To call a void function from another file in Flutter, what you have to do are:

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


Create a file called test.dart in the lib folder:Advertisements

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: Text('Button'), onPressed: doSomething,)

Hope this helps!

Further reading:

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

Related Articles

Latest Articles