Skip to content

Added null check in equals function and updated tests.#4

Merged
suragch merged 1 commit into
suragch:masterfrom
ScottAtRedHawk:string-equals-null
Oct 11, 2023
Merged

Added null check in equals function and updated tests.#4
suragch merged 1 commit into
suragch:masterfrom
ScottAtRedHawk:string-equals-null

Conversation

@ScottAtRedHawk

Copy link
Copy Markdown

No description provided.

@ScottAtRedHawk

Copy link
Copy Markdown
Author

Here is example of the use case this fixes:

void main() {
  const String? optionalStr = null;
  
  print(equals('null', optionalStr)); // true
  print(equalsNew('null', optionalStr)); // false
}

bool equals(String str, Object? comparison) {
  return str == comparison.toString();
}

bool equalsNew(String str, Object? comparison) {
  if (comparison == null) {
    // any non-nullable String will never equal null
    // this still a valid use case though
    return false;
  }
  
  return str == comparison.toString();
}

@suragch
suragch merged commit f5b823b into suragch:master Oct 11, 2023
@suragch

suragch commented Oct 11, 2023

Copy link
Copy Markdown
Owner

Good catch. Thank you for your PR!

@suragch

suragch commented Oct 11, 2023

Copy link
Copy Markdown
Owner

Published as version 1.0.1.

@ScottAtRedHawk
ScottAtRedHawk deleted the string-equals-null branch October 11, 2023 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants