Skip to content

Need null check after ??= #51092

Closed
Closed
@Lysmi

Description

@Lysmi

If I use "??=" dart analyzer don't understand that variable can not be null.
Example:

static void regCommand(String command, Function action, {bool? isMustRemove}) {
      teleDart.onCommand(command).listen((message) {
          isMustRemove ??= false;
          if (isMustRemove) {
              teleDart.deleteMessage(message.chat.id, message.message_id);
          }
    });
  }

This code have error:
image

But in reality isMustRemove can not be null. Of course, if I use {bool isMustRemove = false} is solve this case, but we have more cases with analyze null.
And if I write:

static void regCommand(String command, Function action, {bool? isMustRemove}) {
      teleDart.onCommand(command).listen((message) {
          isMustRemove ??= false;
          if(isMustRemove != null) {
              if (isMustRemove) {
                  teleDart.deleteMessage(message.chat.id, message.message_id);
              }
          }
    });
  }

or

static void regCommand(String command, Function action, {bool? isMustRemove}) {
      teleDart.onCommand(command).listen((message) {
          isMustRemove ??= false;
          if (isMustRemove as bool) {
              teleDart.deleteMessage(message.chat.id, message.message_id);
          }
    });
  }

all work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-duplicateClosed in favor of an existing report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions