add method Null safety

  1. @override
BoxBorder? add(
  1. ShapeBorder other,
  2. {bool reversed = false}

Attempts to create a new object that represents the amalgamation of this border and the other border.

If the type of the other border isn't known, or the given instance cannot be reasonably added to this instance, then this should return null.

This method is used by the operator + implementation.

The reversed argument is true if this object was the right operand of the + operator, and false if it was the left operand.


BoxBorder? add(ShapeBorder other, { bool reversed = false }) {
  if (other is BorderDirectional) {
    final BorderDirectional typedOther = other;
    if (BorderSide.canMerge(top, &&
        BorderSide.canMerge(start, typedOther.start) &&
        BorderSide.canMerge(end, typedOther.end) &&
        BorderSide.canMerge(bottom, typedOther.bottom)) {
      return BorderDirectional.merge(this, typedOther);
    return null;
  if (other is Border) {
    final Border typedOther = other;
    if (!BorderSide.canMerge(, top) ||
        !BorderSide.canMerge(typedOther.bottom, bottom))
      return null;
    if (start != BorderSide.none ||
        end != BorderSide.none) {
      if (typedOther.left != BorderSide.none ||
          typedOther.right != BorderSide.none)
        return null;
      assert(typedOther.left == BorderSide.none);
      assert(typedOther.right == BorderSide.none);
      return BorderDirectional(
        top: BorderSide.merge(, top),
        start: start,
        end: end,
        bottom: BorderSide.merge(typedOther.bottom, bottom),
    assert(start == BorderSide.none);
    assert(end == BorderSide.none);
    return Border(
      top: BorderSide.merge(, top),
      right: typedOther.right,
      bottom: BorderSide.merge(typedOther.bottom, bottom),
      left: typedOther.left,
  return null;