larryq
larryq

Reputation: 16309

Is this a static method, or something else?

I'm learning Dart so that I can do some flutter mobile development. Dart's pretty straightforward to learn and I like it, though there are a few differences from Java / C# that I have to work through.

One of them has to do with this code:

class CatalogSlice {

  final List<CatalogPage> _pages;

  final int startIndex;

  final bool hasNext;

  CatalogSlice(this._pages, this.hasNext)
      : startIndex = _pages.map((p) => p.startIndex).fold(0x7FFFFFFF, min);

  const CatalogSlice.empty()
      : _pages = const [],
        startIndex = 0,
        hasNext = true;
}

Ignoring all the business-specific stuff in there about what a CatalogSlice represents, I'm confused about the definition of the empty() method. Is that a static, class method, or something else?

Upvotes: 1

Views: 43

Answers (1)

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76273

It's a named constructor. You call it the same way you call the generative constructor.

var instance1 = new CatalogSlice(pages, hasNext);
var instance2 = new CatalogSlice.empty();

Dart doesn't have method/constructor overloading (yet) and that's why they introduced this feature.

Upvotes: 3

Related Questions