skip to Main Content

My ListView.builder gives an error saying

The left operand can't be null, so the right operand is never executed.
Try removing the operator and the right operand

The return type 'Object' isn't a 'Widget', as required by the closure's context

Can someone explain what I did wrong? Or is there a way to make PictureTile() or NotificationTile1() nullable

newNotifications

List newNotification = ['liked', 'follow'];

ListView.builder

ListView.builder(
              itemCount: newNotification.length,
              itemBuilder: (context, index) {
                return newNotification[index] == 'follow' ?? PictureTile():NotificationTile1();
            },),

Picture tile

class PictureTile extends StatelessWidget {
  const PictureTile({super.key});

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        SizedBox(
          height: 70,
          width: 70,
          child: Stack(
            children: const [
              Padding(
                padding: EdgeInsets.only(left: 10),
                child: CircleAvatar(
                  radius: 25,
                  backgroundImage:
                      AssetImage('assets/images/ico-modified-rem.png'),
                ),
              ),
              Positioned(
                bottom: 10,
                child: CircleAvatar(
                  radius: 25,
                  backgroundImage: AssetImage('assets/images/sha-modified.png'),
                ),
              ),
            ],
          ),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: const [
              Text(
                'Order Confirmation',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
              SizedBox(
                height: 5,
              ),
              Text(
                'Order Cancelled',
                style: TextStyle(
                  fontSize: 13,
                  color: Colors.black54,
                ),
              ),
              const SizedBox(height: 5),
              Text(
                'You ordered Shawarma',
                style: TextStyle(
                  fontSize: 13,
                ),
              ),
            ],
          ),
        ),
        Image.asset(
          'assets/images/chicken-shawarma-modified.png',
          height: 95,
          width: 91,
        ),
      ],
    );
  }
}

Notification tile

class NotificationTile1 extends StatefulWidget {
  const NotificationTile1({super.key});

  @override
  State<NotificationTile1> createState() => _NotificationTile1State();
}

class _NotificationTile1State extends State<NotificationTile1> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        TileImg('assets/images/ico-modified-rem.png'),
        const SizedBox(
          width: 15,
        ),
        Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: const [
            Text(
              'Order Confirmation',
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
            ),
            SizedBox(
              height: 5,
            ),
            Text(
              'Order Successful',
              style: TextStyle(
                fontSize: 13,
                color: Colors.black54,
              ),
            ),
            SizedBox(
              height: 5,
            ),
            Text(
              'You ordered Shawarma',
              style: TextStyle(
                fontSize: 13,
              ),
            ),
          ],
        ),
      ],
    );
  }
}

2

Answers


  1. Change

    return newNotification[index] == 'follow' ?? PictureTile():NotificationTile1();
    

    to

    return newNotification[index] == 'follow' ? PictureTile():NotificationTile1();
    

    Or You can possibly use:

    ListView.builder(
      itemCount: newNotification.length,
      itemBuilder: (context, index) {
        if (newNotification[index] == 'follow') {
          return PictureTile();
        } else {
          return NotificationTile1();
        }
      },
    )
    
    Login or Signup to reply.
  2. Replace the code

    return newNotification[index] == 'follow' ?? PictureTile():NotificationTile1();
    

    with

    return newNotification[index] == 'follow' ? PictureTile():NotificationTile1();
    

    Using (short if) is the best efficient way, also check this blog which will give a lot of information about operators.
    if its work don’t forget to vote 🙏️.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search