Ali K
Ali K

Reputation: 325

The operator '>' can't be unconditionally invoked because the receiver can be 'null'

          itemCount: (widget.userbio.resimler?.length!=null) ? 0:(widget.userbio.resimler?.length!>9)?9:widget.userbio.resimler?.length,

error:

error: The operator '>' can't be unconditionally invoked because the receiver can be 'null'. (unchecked_use_of_nullable_value at [ui_profile_instagram] lib\pages\profile.dart:220)

hello how to fix this error ?

widget.userbio.resimler?.length!>9

ı need compare

Upvotes: 0

Views: 49

Answers (1)

Richard Heap
Richard Heap

Reputation: 51682

The short answer is to change it to widget.userbio.resimler!.length!.

The long answer is to introduce a local variable. Something like:

final l = widget.userbio.resimler?.length;

You can then test l for null and the compiler will know that after you've tested it, it can't be null. (Note this only works for local variables - hence why you should add one.)

if (l != null) {/* in this block l cannot be null*/}

so

final l = widget.userbio.resimler?.length;
itemCount: l == null ? 0 : l > 9 ? 9 : l;

Best of all user a null aware operator:

var l = widget.userbio.resimler?.length ?? 0;
if (l > 9) l = 9;

Upvotes: 1

Related Questions