Static variables and Static methods are used to reduce memory usages in any programming languages. Static variables can create once and used many times without creating class Object. So Static variables and get memory once on declaration time. Static variables and Static methods are called without object directly with Class name with Dot operator. Static variables are also known as Class variables. Static methods are also known as Class methods. So in this tutorial we would learn about Flutter Dart Create Call Static Variables Static Methods Android iOS Example Tutorial.
Contents in this project Flutter Dart Create Call Static Variables Static Methods Android iOS Example Tutorial:
1. Import material.dart package in your app’s main.dart file.
1 |
import 'package:flutter/material.dart'; |
2. Creating void main runApp() method and here we would call our main MyApp main class.
1 |
void main() => runApp(MyApp()); |
3. Creating a class named as Holder. In this class we would make a Static Double variable named as number and a Static function named as message. We are simply printing a message on Terminal screen using Print function.
1 2 3 4 5 6 7 8 9 10 |
class Holder { static double number = 12345 ; static void message(){ print('You are Calling Static Method'); } } |
4. Creating our main MyApp class extends with StatelessWidget. This is our main Widget View class.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
5. Creating 2 function named as callStaticMember() and callStaticFunction(). In first function we would printing Static double variable on terminal screen and in other function we would call the message function with class name.
Note: To call the Static function and Static member variable we would use Class name + Dot operator + Variable Name or Function name Syntax.
1 2 3 4 5 6 7 8 9 10 11 |
void callStaticMember() { print(Holder.number); } void callStaticFunction() { Holder.message(); } |
6. Creating Widget Build area in MyApp class. Now we would make 2 Raised Buttons widget and call above declared both function onPress event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(20, 0, 20, 0), child: RaisedButton( onPressed: () => callStaticMember(), child: Text(' Click Here To Call Static Variable '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => callStaticFunction(), child: Text(' Click Here To Call Static Function '), textColor: Colors.white, color: Colors.lightBlue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } |
7. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class Holder { static double number = 12345 ; static void message(){ print('You are Calling Static Method'); } } class MyApp extends StatelessWidget { void callStaticMember() { print(Holder.number); } void callStaticFunction() { Holder.message(); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(20, 0, 20, 0), child: RaisedButton( onPressed: () => callStaticMember(), child: Text(' Click Here To Call Static Variable '), textColor: Colors.white, color: Colors.green, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), Container( margin: const EdgeInsets.fromLTRB(20, 10, 20, 10), child: RaisedButton( onPressed: () => callStaticFunction(), child: Text(' Click Here To Call Static Function '), textColor: Colors.white, color: Colors.lightBlue, padding: EdgeInsets.fromLTRB(12, 12, 12, 12), ) ), ]) ) ) ); } } |
