Skip to content

possibly warn when a class extends ListBase in null safe code, and doesn't implement add? #46646

Open
@annagrin

Description

@annagrin

Using

import 'dart:collection';

void main() {
  {
    var list = [];
    // works
    list.length = 1;
    print('Length: ${list.length}');
  }

  {
    var list = <Object>[];
    // throws
    list.length = 1;
    print('Length: ${list.length}');
  }
}

Expected
I suspect that the list cannot grow by adding Null elements due to null safety, so the code should throw, but it would be great to have a better error message. Even better, is it possible to give a compilation error instead?

Actual

Unhandled exception:
    type 'Null' is not a subtype of type 'Object' in type cast
    #0      List.length= (dart:core-patch/growable_array.dart:222:12)
    #1      main (file:///Users/annagrin/source/try/notAListBug/main.dart:14:10)
    #2      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
    #3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

Dart SDK version
Dart SDK version: 2.14.0-321.0.dev (dev) (Thu Jul 15 08:55:11 2021 -0700) on "macos_x64"

Platform
VM and web

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-warningIssues with the analyzer's Warning codestype-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