skip to Main Content

Why I keep getting this error message although I added the null checking?

 if (params.page != null) {
      ++params.page;
  }

Error

The method '+' can't be unconditionally invoked because the receiver can be 'null'.
Try making the call conditional (using '?.') or adding a null check to the target ('!')

3

Answers


  1. You’re probably trying to promote a field that is not private. As per Dart 3.2, type promotion for fields only works when the field is private.

    See: Only private fields can be promoted

    Login or Signup to reply.
  2. Try this one

    if (params.page != null) {
        params.page++;
    }
    
    Login or Signup to reply.
  3. Sure, I can help with that :

    1: if ( params.page != null ) { params.page!++ ; // Using the non-null assertion

    }

    or this

    2: if ( params.page != null ) { params.page?.++ ; // Using the non – null

    assertion }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search