Ahmed Kamal
Ahmed Kamal

Reputation: 3000

Is it possible to have a private constructor in dart?

I'm able to do something like the following in TypeScript

class Foo {
  private constructor () {}
}

so this constructor is accessible only from inside the class itself.

How to achieve the same functionality in Dart?

Upvotes: 107

Views: 47734

Answers (6)

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4769

Yes, dart provides capability to define a private constructor.

And the best use case to use this is to define factory method.

Let me explain with example.

class AlertDialogScreen {

AlertDialogScreen._({
    required this.title,
    required this.body,
    required this.alertType,
    this.confirmButtonText = '',
    this.cancelButtonText = '',
  });
}

final String title;
  final String body;
  final AlertDialogType alertType;
  final String confirmButtonText;
  final String cancelButtonText;

And we have AlertDialogType

enum AlertDialogType { info, confirmation }

Now we understand that the AlertDialogScreen can be of two types, either of type info or of type alert.

Hence the constructor is defined as private. Notice AlertDialogScreen._(

And this private constructor can be called from within the class. And in my use case, we have two factory methods to create different type of alerts.

factory AlertDialogScreen.infoAlert({
    required String title,
    required String body,
  }) {
    return AlertDialogScreen._(
      title: title,
      body: body,
      alertType: AlertDialogType.info,
    );
  }

and confirmation type

factory AlertDialogScreen.confirmationAlert({
    required String title,
    required String body,
    required String confirmButtonText,
    required String cancelButtonText,
  }) {
    return AlertDialogScreen._(
      title: title,
      body: body,
      confirmButtonText: confirmButtonText,
      cancelButtonText: cancelButtonText,
      alertType: AlertDialogType.confirmation,
    );
  }

This pattern handles the optional arguments while preparing the required object in better way, and is more readable.

Upvotes: 0

Muhammad Muddassar
Muhammad Muddassar

Reputation: 555

you can create following class in order to get a singleton instance

class Sample{
    factory Sample() => _this;
    Sample._(); // you can add your custom code here
    static final Sample _this = Sample._();
}

Now in the main function you can call the sample constructor

void main(){
    /// this will return the _this instace from sample class
    Sample sample = Sample(); 

}

Upvotes: 7

just use abstract class. Because you can't instantiate abstract class

Upvotes: -6

Jitesh Mohite
Jitesh Mohite

Reputation: 34210

Yes, It is possible, wanna add more information around it.

A constructor can be made private by using (_) underscore operator which means private in dart.

So a class can be declared as

class Foo {
  Foo._() {}
}

so now, The class Foo doesn't have a default constructor

Foo foo = Foo(); // It will give compile time error

The same theory applied while extending class also, It's also impossible to call the private constructor if it declares in a separate file.

class FooBar extends Foo {
    FooBar() : super._(); // This will give compile time error.
  }

But both above functionality works if we use them in the same class or file respectively.

  Foo foo = Foo._(); // It will work as calling from the same class

and

 class FooBar extends Foo {
    FooBar() : super._(); // This will work as both Foo and FooBar are declared in same file. 
  }

Upvotes: 24

cesar
cesar

Reputation: 301

A method without any code must be something like this

class Foo {
  Foo._();
}

Upvotes: 30

Mattia
Mattia

Reputation: 6524

Just create a named constructor that starts with _

class Foo {
  Foo._() {}
}

then the constructor Foo._() will be accessible only from its class (and library).

Upvotes: 195

Related Questions