Search code examples
flutterclassdartstaticstatic-methods

why we should use static keyword in dart in place of abstract?


I m preparing a class in my flutterfire project and their I want to use some method Which can't change further so that I want to know consept of static keyword in Dart ?


Solution

  • "static" means a member is available on the class itself instead of on instances of the class. That's all it means, and it isn't used for anything else. static modifies members.

    Static methods Static methods (class methods) don’t operate on an instance, and thus don’t have access to this. They do, however, have access to static variables.

    void main() {
    
     print(Car.numberOfWheels); //here we use a static variable.
    
    //   print(Car.name);  // this gives an error we can not access this property without creating  an instance of Car class.
    
      print(Car.startCar());//here we use a static method.
    
      Car car = Car();
      car.name = 'Honda';
      print(car.name);
    }
    
    class Car{
    static const numberOfWheels =4;
      Car({this.name});
      String name;
      
    //   Static method
      static startCar(){
        return 'Car is starting';
      }
      
    }