How to handle Null value in Flutter ? | Handle Grey screen in Flutter

researchthinker

Null value in Flutter

Null value occurs in different ways in flutter, sometimes null value is not controlled in production which causes grey screen and sometimes null value occurs in API response and many more reasons, To deal with this, we have added three ways through which you can handle it.

1. Tertiary operator

Text (nameOfEmployee!=null? nameOfEmployee: "")

#Here in this case nameOfEmployee sometime value is null,To handle this we can use tertiary operator

2.Empty Widget

//This will hide Grey screen if null occur
nameOfEmployee!=null ? Text (nameOfEmployee):
Container(
        alignment: Alignment.center,
        child: EmptyWidget(
          // Image from project assets
          image: "assets/images/im_emptyIcon_1.png",//Any image

          /// Image from package assets
          /// Uncomment below line to use package assets
          packageImage: PackageImage.Image_1,
          title: 'No Notification',
          subTitle: 'No  notification available yet',
          titleTextStyle: TextStyle(
            fontSize: 22,
            color: Color(0xff9da9c7),
            fontWeight: FontWeight.w500,
          ),
          subtitleTextStyle: TextStyle(
            fontSize: 14,
            color: Color(0xffabb8d6),
          ),
          // Uncomment below statement to hide background
          // hideBackgroundAnimation: true,
        ),
      ),

3 if null | Double Operator ??

Text (nameOfEmployee ?? "Nothing") # in this if nameOfEmployee =null then automatically Text widget display Nothing

Leave a Reply

Your email address will not be published. Required fields are marked *

web_horizontal
About Us ♢ Disclaimer ♢ Privacy Policy ♢ Terms & Conditions ♢ Contact Us

Copyright © 2023 ResearchThinker.com. All rights reserved.