Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NNBD: How to increment/decrement a nullable expression using the operators ++/--? #1221

Closed
simphotonics opened this issue Sep 13, 2020 · 2 comments
Labels
state-duplicate This issue or pull request already exists

Comments

@simphotonics
Copy link

What is the syntax for incrementing/decrementing a nullable expression (such as the value of a map entry) using the prefix/postfix operators ++ or -- ? I am getting the error shown below.

$ dart --version
Dart SDK version: 2.9.3 (stable) (Unknown timestamp) on "linux_x64"
final map = <int, int>{};
var index = 0;
...

if (map[index] == null){
  map[index] = 1;
}else{
  map[index] = map[index]! + 1;  // This works.
   ++map[index]!; // Missing selector such as '.identifier' or '[0]'. Try adding a selector.dart(missing_assignable_selector)
   ++(map[index]!); // Missing selector such as '.identifier' or '[0]'. Try adding a selector.dart(missing_assignable_selector)
   map[index]! += 1; // Missing selector such as '.identifier' or '[0]'. Try adding a selector.dart(missing_assignable_selector)
}

It seems that the expression map[index]! is not assignable.

@eernstg eernstg added the state-duplicate This issue or pull request already exists label Sep 14, 2020
@eernstg
Copy link
Member

eernstg commented Sep 14, 2020

This discussion has been raised in #1113. There is currently no mechanism which will allow you to combine null checks (!) with increment/decrement, but please continue the discussion there.

@eernstg eernstg closed this as completed Sep 14, 2020
@simphotonics
Copy link
Author

Thank you for pointing out #1113.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants