2

I am reading some code example online and see this:

class _CirclePainter extends BoxPainter {
  final Paint _paint;
  final double radius;

  _CirclePainter(Color color, this.radius)
      : _paint = Paint()
          ..color = color
          ..isAntiAlias = true;

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration cfg) {
    final Offset circleOffset =
        offset + Offset(cfg.size.width / 2, cfg.size.height - radius - 5);
    canvas.drawCircle(circleOffset, radius, _paint);
  }
}

Can someone please explain what are the semicolon and the double dots? I only know "(condition) ? (do this) : (do that)".

  _CirclePainter(Color color, this.radius)
  : _paint = Paint()
      ..color = color
      ..isAntiAlias = true;

Thanks.

EDIT: I meant colon, not semicolon.

57659
  • 131
  • 1
  • 1
  • 6

3 Answers3

1

.. is known as cascade operator in dart. It's basically used to add or modify multiple properties while initializing a object

var paint = Paint() ..color = color ..isAntiAlias = true;

var paint =Paint(); paint.color = color; paint.isAntiAlias = true;

Both are same just a short method to write

More info How do method cascades work exactly in dart?

Sachin Bhankhar
  • 900
  • 8
  • 14
0

Semi colon means end of the statement double dots is used in order to point different target and prevents from calling the same target if your intention is to call several methods of same object

Pratyay Sinha
  • 511
  • 1
  • 6
  • 16
0

: is again a shorthand for your constructor

so instead of

  _CirclePainter(Color color, this.r) {
    _paint = Paint()..color = color;
    // `..` short hand for accessing the object
  }

you use

  _CirclePainter(Color color, this.r) 
    : _paint = Paint()..color = color;

this is extensive used in Flutter code, mostly for assignments and assertion For instance the Container either takes a color or decoration and the constructor for that looks like this

Container({
  ...
  this.color,
  this.decoration,
  }) : 
     ...
     assert(color == null || decoration == null,
       'Cannot provide both a color and a decoration\n'
       'To provide both, use "decoration: BoxDecoration(color: color)".'
     ),
     super(key: key);

https://api.flutter.dev/flutter/widgets/Container/Container.html

Prathik
  • 474
  • 3
  • 13