Flutter Create Call Function From Another Class in Main Class Dart Example

Dart is like other programming languages which supports all type of typical methods. Like using another class functions from default class by creating their Object. Object works as Class reference or instance which created by assigning class directly to a Variable in dart. Object is a real time entity used to access all the parent class members in calling child class. So in this tutorial we would Flutter Create Call Function From Another Class in Main Class in Dart Android iOS Example Tutorial.

Contents in this project Flutter Create Call Function From Another Class in Main Class Dart Android iOS Example Tutorial:

1. Import material.dart package in your app’s main.dart file.

2. Create void main runApp() method and here we would call our main MyApp class.

3. Create a standalone Class named as Second. This is our class in which we are making 3 different functions.

  1. randomMSG : Used to print some random message on Terminal screen using print method.
  2. printText : Using this method first user would pass some string message as argument and then we would print this message on Terminal screen using print.
  3. returnMSG : At the message calling time we would pass two different values as function argument and then we would calculate these values and return total sum of these.

5. Create our main class named as MyApp extends with State less widget.

5. Create a final type variable named as second and here we would assign the class instance to second. Now the second works as Second class object.

6. Create a function named as fn_1(). Inside this function we would call randomMSG() function of Second class using second object with Dot operator.

7. Create a function named as fn_2(). Inside the function we would call the second.printText(‘ ‘) function with some string argument. We would also call this function using second object.

8. Create a function named as fn_3(). Inside the function we would call the returnMSG() function using second object and pass 2 double values along with it as argument. When the sum is done by second class function it will return us the total and we would store the total in holder variable and than print the holder on screen.

9. Creating Widget build area in MyApp class. Now we would make 3 Raised buttons and call above all three functions on button onPress event.

10. Complete source code for main.dart file:

Screenshots:

Flutter Create Call Function From Another Class in Main Class Dart Example

Leave a Reply

Your email address will not be published. Required fields are marked *