Static Variables Static Methods Using Flutter Dart

admin_img Posted By Bajarangi soft , Posted On 05-11-2020

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.

Static Variables Static Methods Using Flutter Dart

Static Variables Static Methods 
Complete Code For Static Variables Static Methods  In Flutter
main.dart

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class Holder {
  static double number = 34567890 ;
  static void message(){
    print('You are Calling Static Method');
  }

}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {

  void callStaticMember() {
    print(Holder.number);
  }

  void callStaticFunction() {
    Holder.message();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.black,
        appBar: AppBar(
          backgroundColor: Colors.blue[800],
          title: Text('Static Variables Static Methods'),
        ),
        body: Center(
            child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                      child: RaisedButton(
                        onPressed: () => callStaticMember(),
                        child: Text(' Call Static Variable '),
                        textColor: Colors.white,
                        color: Colors.green,
                      )
                  ),
                  Container(
                      child: RaisedButton(
                        onPressed: () => callStaticFunction(),
                        child: Text(' Call Static Function '),
                        textColor: Colors.white,
                        color: Colors.blue[800],
                      )
                  ),

                ])
        )
    );
  }
}

Related Post