Text Widget in ‘package:flutter/src/widgets/text.dart’ requires a non-null String at line 378 position 10: Assertion failed

If the variable is null, the default text will be displayed in the mobile screenshot. The error location for my_text.dart is shown. To solve the issue, check if the variable is null before using it in the Text widget. This is the first solution.


Question:

Can anyone assist me with an error message that appears in red whenever I try to access the notification details on
flutter app
?

I am in need of assistance as I am unable to locate any solutions at this time.

There is some uncertainty regarding whether this code is the root cause of the issues.

        Align(
          alignment: Alignment.centerRight,
          child: IconButton(
            icon: Icon(Icons.close),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ),
        Container(
          height: 150, width: MediaQuery.of(context).size.width,
          margin: EdgeInsets.symmetric(horizontal: Dimensions.PADDING_SIZE_LARGE),
          decoration: BoxDecoration(borderRadius: BorderRadius.circular(Dimensions.RADIUS_SMALL), color: Theme.of(context).primaryColor.withOpacity(0.20)),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(Dimensions.RADIUS_SMALL),
            child: FadeInImage.assetNetwork(
              placeholder: Images.placeholder,
              image: '${Get.find().configModel.baseUrls.notificationImageUrl}/${notificationModel.image}',
              height: 150, width: MediaQuery.of(context).size.width, fit: BoxFit.cover,
              imageErrorBuilder: (c, o, s) => Image.asset(
                Images.placeholder, height: 150,
                width: MediaQuery.of(context).size.width, fit: BoxFit.cover,
              ),
            ),
          ),
        ),
        SizedBox(height: Dimensions.PADDING_SIZE_LARGE),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: Dimensions.PADDING_SIZE_LARGE),
          child: Text(
            notificationModel.title,
            textAlign: TextAlign.center,
            style: robotoMedium.copyWith(color: Theme.of(context).primaryColor, fontSize: Dimensions.FONT_SIZE_LARGE),
              ),
        ),
        Padding(
          padding: EdgeInsets.fromLTRB(20, 10, 20, 20),
          child: Text(
            notificationModel.description,
            textAlign: TextAlign.center,
            style: robotoRegular.copyWith(color: Theme.of(context).disabledColor),
          ),
        ),
      ],
    ),
  ),
);

}
}

The code has been updated to display a default text if the variable is null using the Text(someVariable ?? ‘Default text if variable is null’) function. A screenshot of the updated mobile view is also available.

my text.dart error location

 assert(
      data != null,
     'A non-null String must be provided to a Text widget.',
   ),
   textSpan = null,
   super(key: key);


Solution 1:

It appears that you are passing a null variable in the Text widget. Therefore, you should ensure to verify the variable before utilizing it in the Text widget.

You can check null safe like:

Text(someVariable ?? 'Default text if varible is null')


Solution 2:


Incorporate the below code, which includes the use of elves operator, into your own code.

Padding(
          padding: EdgeInsets.symmetric(horizontal: Dimensions.PADDING_SIZE_LARGE),
          child: Text(
            notificationModel.title ?? "Text value is null",
            textAlign: TextAlign.center,
            style: robotoMedium.copyWith(color: Theme.of(context).primaryColor, fontSize: Dimensions.FONT_SIZE_LARGE),
              ),
        ),

Frequently Asked Questions