Is this a static method, or something else?

1

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?

dart
asked on Stack Overflow Oct 17, 2018 by larryq

1 Answer

3

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.

answered on Stack Overflow Oct 17, 2018 by Alexandre Ardhuin • edited Oct 17, 2018 by Alexandre Ardhuin

User contributions licensed under CC BY-SA 3.0