Skip to content

We need to migrate to null-safety #2737

Closed
@jonataslaw

Description

@jonataslaw

Hi, I would like to propose switching to null-safety.
I can contribute to this if there is no blockage.
I noticed that there are many packages in pub.dev that have their score downgraded because of dartdoc issues, and these issues could be easily fixed if we had null security, which prevents new null bugs from being inserted with every update.
Looking at the source, I can see for example that there are many ifs without else that can potentially cause bugs, for example:

https://github.com/dart-lang/dartdoc/blob/master/lib/src/model/type_parameter.dart#L38

  ElementType get boundType {
    if (_boundType == null) {
      var bound = element.bound;
      if (bound != null) {
        _boundType = ElementType.from(bound, library, packageGraph);
      }
    }
    return _boundType;
  }

boundType cannot be null since we use this class's getter all the time, for example:

https://github.com/dart-lang/dartdoc/blob/master/lib/src/model/type_parameter.dart#L71

  @override
  Map<String, CommentReferable> get referenceChildren {
    return _referenceChildren ??= {
      boundType.name: boundType, // boundType.name will cause a null exception
    };
  }

However, let's look at this code:

ElementType get boundType {
    if (_boundType == null) { // _boundType is null
      var bound = element.bound;
      if (bound != null) { // if bound is not null, we add ElementType.from bound
        _boundType = ElementType.from(bound, library, packageGraph);
      }
       /// However if bound is null, we don't assign any value to _boundType
    }
   /// and here we return null.
    return _boundType;
  }

This is causing a ton of errors in pub.dev packages. We can take, for example, the most liked package from pub.dev:
https://pub.dev/packages/get/score

Unhandled exception:
NoSuchMethodError: The getter 'name' was called on null.
Receiver: null
Tried calling: name

This is for the reason mentioned above. The problem is that this isn't the only place we have if without elses delivering null, and the solution to that would be to migrate to null-safety.

Is there a block for this? Otherwise I can do a PR with the migration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1A high priority bug; for example, a single project is unusable or has many test failurestype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions