jakub
jakub

Reputation: 3844

Dart null safety and smart detecting non nulls

I have problems with Dart null safety.

Even when I explicitly check for nulls, it still complainsenter image description here

The exclamation mark is solving that

enter image description here

but it's pretty ugly and cumbersome.

Is there a way to make it smarter and nicer? Like e.g. header?.let{ it.toUpperCase() } in Kotlin?

Upvotes: 1

Views: 193

Answers (1)

glavigno
glavigno

Reputation: 503

I suppose that header has a type of String?. It seems that variables used in logical expressions must be part of the local scope of the current context. In your example, passing down header as a String? typed argument allow your to access String methods if its value is not null.

  Widget _buildHeader(String? header) {
    if (header != null) {
      return Text(h.toUpperCase());
    }
    return Container();
  }

Upvotes: 2

Related Questions