What is the Dart null checking idiom or best practice?

前端 未结 4 2129
天命终不由人
天命终不由人 2020-12-04 15:25

I have the following form of assignment & null checks to avoid double lookups in my maps.
Is there a better or more idiomatic way to do this in Dart?



        
4条回答
  •  盖世英雄少女心
    2020-12-04 15:54

    There are now 4 null aware operators

    ??     provides a default if the subject is null

    return subject ?? "defaultIfNull";
    

    ??=     sets the subject to a default only if the subject is null

    This is similar to ?? but sets the subject variable to a default if it is null.

    subject ??= "defaultIfNull";
    

    ?.     avoid an exception if subject is null when accessing subject's property

    object?.x will return null if object is null, object.x would cause an exception if object were null

    ...?     from a spread collection, avoid a null item in the final list if the subject list is null

    the result of the following

    [
      ...[1, 2],
      null,
    ]
    

    is [1, 2, null]

    to avoid the null value use ...?

    var resultingList = [
      ...[1, 2],
      ...?subjectList,
    ];
    

提交回复
热议问题