Checking null inside a widget in Flutter

This is Flutter series, if you not familiar with Flutter development here is the post for you.

Encounter and treat null is a should have in development cycle. We know how to deal with null around class, objects and functions too. So how about widget and properties.

I have customized a Material button (I will pull that stuff in the next post) which accepting different type of borders, StadiumBorder() like stuff. By default I want to set StadiumBorder, if user not passing any values. Possibly fire an error on default value for shape.

So how do we check null ?

Here is the solution

shape:newshape!=null ? newshape:  const StadiumBorder(),

I check for the new value for shape , if it fails I use a default one. Quiet simple , isn’t it.

Published by


A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.