4

In Example at https://pub.dev/packages/contacts_service/versions/0.4.3/example there is a code like as follows

itemCount: _contacts?.length ?? 0,

and I have two question on that,

what is meant by variableName? ( variableName followed by question mark '?') e.g in above _contacts?

The second question was on two question marks and is at What are the ?? double question marks in Dart?

mousetail
  • 7,009
  • 4
  • 25
  • 45
puzzled
  • 509
  • 1
  • 5
  • 18
  • https://dart.dev/guides/language/language-tour#other-operators – jamesdlin Feb 25 '21 at 05:33
  • itemCount: _contacts?.length ?? 0. Answer (1) A nullable type by adding a question mark (?) after the type name. For example, a variable _contacts? can contain a value, or it can be null. Answer (2) It is called "Null-aware operators". When ??= is used, to assign a value to an object If that object is null. Or return the object. – Subarata Talukder Feb 23 '22 at 09:00

1 Answers1

1

"Variable?" is use for null safety. If _contact has value null then we can't check length of variable or it's throw error.

"??" is used for if condition. It's assign value 0.

Check Here for more information.

Jay Kukadiya
  • 553
  • 6
  • 9
  • [link](https://medium.com/dartlang/announcing-dart-2-12-499a6e689c87) check this link for more info – Jay Kukadiya Mar 06 '21 at 04:24
  • It should throw error irrespective of question mark is there or not? right?. null value should not have any length. Is there any example where this might be useful. – Mayank Pant Apr 02 '22 at 16:31