Home > Blockchain >  Flutter 2.8.1 Null check operator used on a null value
Flutter 2.8.1 Null check operator used on a null value

Time:02-24

I have a flutter grocery application that was working fine till my last build but the latest build is giving me the following error.

I am working on a stable channel and already tried cache repair and doing a clean build The error still persists.

  The following _CastError was thrown building StreamBuilder<List<Product>>(dirty, dependencies: [_InheritedProviderScope<ThemeModel>, MediaQuery], state: _StreamBuilderBaseState<List<Product>, AsyncSnapshot<List<Product>>>#ed27f):
Null check operator used on a null value
The relevant error-causing widget was: 
  StreamBuilder<List<Product>> StreamBuilder:file:///Users/Osamah/Downloads/grocery_user_fixed-master/lib/ui/home/cart/cart.dart:68:20
When the exception was thrown, this was the stack: 
#0      Cards.cart (package:grocery/widgets/cards.dart:203:76)
#1      _CartState.build.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:grocery/ui/home/cart/cart.dart:140:48)
#2      new _GrowableList.generate (dart:core-patch/growable_array.dart:133:28)
#3      _CartState.build.<anonymous closure>.<anonymous closure> (package:grocery/ui/home/cart/cart.dart:138:40)
#4      StreamBuilder.build (package:flutter/src/widgets/async.dart:442:81)
#5      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48)
#6      StatefulElement.build (package:flutter/src/widgets/framework.dart:4705:27)
#7      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4588:15)
#8      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#9      Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#10     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2578:33)
#11     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#12     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#13     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1145:15)
#14     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
#15     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:996:5)
#19     _invoke (dart:ui/hooks.dart:150:10)
#20     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:270:5)
#21     _drawFrame (dart:ui/hooks.dart:114:31)
(elided 3 frames from dart:async)

Here is the code section where the error is said to be identified cart.dart

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:grocery/blocs/cart_bloc.dart';
import 'package:grocery/models/data_models/cart_item.dart';
import 'package:grocery/models/state_models/home_model.dart';
import 'package:grocery/models/data_models/product.dart';
import 'package:grocery/models/state_models/theme_model.dart';
import 'package:grocery/ui/home/cart/checkout/checkout.dart';
import 'package:grocery/ui/product_details/product_details.dart';
import 'package:grocery/widgets/buttons.dart';
import 'package:grocery/widgets/cards.dart';
import 'package:grocery/widgets/dialogs.dart';
import 'package:grocery/widgets/fade_in.dart';
import 'package:grocery/widgets/texts.dart';
import 'package:provider/provider.dart';

class Cart extends StatefulWidget {
  final CartBloc bloc;

  const Cart({required this.bloc});

  static Widget create() {
    return Consumer<CartBloc>(
      builder: (context, bloc, _) {
        return Cart(
          bloc: bloc,
        );
      },
    );
  }

  @override
  _CartState createState() => _CartState();
}

class _CartState extends State<Cart> with TickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    final themeModel = Provider.of<ThemeModel>(context);
    double width = MediaQuery.of(context).size.width;

    return StreamBuilder<List<CartItem>>(
      stream: widget.bloc.cartItems,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          List<CartItem> cartItems = snapshot.data!;
          if (cartItems.length == 0) {
            return FadeIn(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    SvgPicture.asset(
                      'images/state_images/empty_cart.svg',
                      width: width * 0.5,
                      fit: BoxFit.cover,
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 30),
                      child: Texts.headline3(
                          'Nothing found here\nGo and enjoy shopping!',
                          themeModel.accentColor,
                          alignment: TextAlign.center),
                    )
                  ]),
            );
          } else {
            return StreamBuilder<List<Product>>(
                stream: widget.bloc.getProducts(cartItems),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    List<Product> products = snapshot.data!;
                    cartItems = cartItems.where((cartItem) {
                      if (products.where((product) {
                            if (cartItem.reference == product.reference) {
                              cartItem.product = product;
                              return true;
                            } else {
                              return false;
                            }
                          }).length ==
                          0) {
                        return false;
                      } else {
                        return true;
                      }
                    }).toList();

                    if (cartItems.length == 0) {
                      return FadeIn(
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              SvgPicture.asset(
                                'images/state_images/empty_cart.svg',
                                width: width * 0.5,
                                fit: BoxFit.cover,
                              ),
                              Padding(
                                padding: EdgeInsets.only(top: 30),
                                child: Texts.headline3(
                                    'Nothing found here\nGo and enjoy shopping!',
                                    themeModel.accentColor,
                                    alignment: TextAlign.center),
                              )
                            ]),
                      );
                    } else {
                      return ListView(
                        padding: EdgeInsets.only(
                            left: 20, right: 20, top: 40, bottom: 80),
                        children: [
                          Padding(
                            padding: EdgeInsets.only(bottom: 20, top: 20),
                            child: Align(
                              alignment: Alignment.center,
                              child:
                                  Texts.headline3("Cart", themeModel.textColor),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(bottom: 20),
                            child: Align(
                              alignment: Alignment.center,
                              child: Texts.descriptiveItems(
                                  "All orders over R600 recieve free delivery",
                                  themeModel.textColor),
                            ),
                          ),

                          ///List of cart items
                          AnimatedSize(
                            vsync: this,
                            duration: Duration(milliseconds: 300),
                            child: Column(
                              children:
                                  List.generate(cartItems.length, (index) {
                                return FadeIn(
                                  child: Cards.cart(context,
                                      goToProduct: () {
                                        ProductDetails.create(
                                                context, products[index])
                                            .then((value) {
                                          if (value != null) {
                                            final homeModel =
                                                Provider.of<HomeModel>(context,
                                                    listen: false);

                                            homeModel.goToPage(0);

                                            showDialog(
                                              context: context,
                                              builder: (context) => Dialogs.success(
                                                  context,
                                                  message:
                                                      "Congratulations!\nYour order is placed!"),
                                            ).then((value) {
                                              widget.bloc.removeCart();
                                            });
                                          }
                                        });
                                      },
                                      cartItem: cartItems[index],
                                      updateQuantity:
                                          widget.bloc.updateQuantity,
                                      updateUnit: widget.bloc.updateUnit,
                                      delete: () async {
                                        await widget.bloc.removeFromCart(
                                            cartItems[index].reference);
                                      }),
                                );
                              }),
                            ),
                          ),

                          ///Checkout button
                          FadeIn(
                            duration: Duration(milliseconds: 400),
                            child: Buttons.button(
                                color: themeModel.accentColor,
                                widget: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Icon(
                                      Icons.arrow_forward,
                                      color: Colors.white,
                                    ),
                                    Padding(
                                      padding: EdgeInsets.only(left: 10),
                                      child: Texts.headline3(
                                          "Checkout", Colors.white),
                                    )
                                  ],
                                ),
                                onPressed: () async {
                                  final checkoutResult =
                                      await Checkout.create(context);
                                  if (checkoutResult ?? false) {
                                    final homeModel = Provider.of<HomeModel>(
                                        context,
                                        listen: false);

                                    homeModel.goToPage(0);

                                    showDialog(
                                      context: context,
                                      builder: (context) => Dialogs.success(
                                          context,
                                          message:
                                              "Congratulations!\nYour order is placed!"),
                                    ).then((value) {
                                      widget.bloc.removeCart();
                                    });
                                  }
                                }),
                          )
                        ],
                      );
                    }
                  } else if (snapshot.hasError) {
                    return FadeIn(
                      child: Center(
                        child: SvgPicture.asset(
                          'images/state_images/error.svg',
                          width: width * 0.5,
                          fit: BoxFit.cover,
                        ),
                      ),
                    );
                  } else {
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  }
                });
          }
        } else if (snapshot.hasError) {
          return FadeIn(
            child: Center(
              child: SvgPicture.asset(
                'images/state_images/error.svg',
                width: width * 0.5,
                fit: BoxFit.cover,
              ),
            ),
          );
        } else {
          return Center(
            child: CircularProgressIndicator(),
          );
        }
      },
    );
  }
}

And here is my flutter doctor output

[✓] Flutter (Channel stable, 2.8.1, on macOS 11.4 20F71 darwin-x64, locale en-PK)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.64.2)
[✓] Connected device (1 available)

• No issues found!

CodePudding user response:

It is not because snapshot has data that data is not null. Try to check snapshot.hasdata and snapshot.data != null

class _CartState extends State<Cart> with TickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    final themeModel = Provider.of<ThemeModel>(context);
    double width = MediaQuery.of(context).size.width;

    return StreamBuilder<List<CartItem>>(
      stream: widget.bloc.cartItems,
      builder: (context, snapshot) {
        if (snapshot.hasData && snapshot.data != null) {
          List<CartItem> cartItems = snapshot.data!;
          if (cartItems.length == 0) {
            return FadeIn(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    SvgPicture.asset(
                      'images/state_images/empty_cart.svg',
                      width: width * 0.5,
                      fit: BoxFit.cover,
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 30),
                      child: Texts.headline3(
                          'Nothing found here\nGo and enjoy shopping!',
                          themeModel.accentColor,
                          alignment: TextAlign.center),
                    )
                  ]),
            );
          } else {
            return StreamBuilder<List<Product>>(
                stream: widget.bloc.getProducts(cartItems),
                builder: (context, snapshot) {
                  if (snapshot.hasData && snapshot.data != null) {
                    List<Product> products = snapshot.data!;
                    cartItems = cartItems.where((cartItem) {
                      if (products.where((product) {
                            if (cartItem.reference == product.reference) {
                              cartItem.product = product;
                              return true;
                            } else {
                              return false;
                            }
                          }).length ==
                          0) {
                        return false;
                      } else {
                        return true;
                      }
                    }).toList();

                    if (cartItems.length == 0) {
                      return FadeIn(
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              SvgPicture.asset(
                                'images/state_images/empty_cart.svg',
                                width: width * 0.5,
                                fit: BoxFit.cover,
                              ),
                              Padding(
                                padding: EdgeInsets.only(top: 30),
                                child: Texts.headline3(
                                    'Nothing found here\nGo and enjoy shopping!',
                                    themeModel.accentColor,
                                    alignment: TextAlign.center),
                              )
                            ]),
                      );
                    } else {
                      return ListView(
                        padding: EdgeInsets.only(
                            left: 20, right: 20, top: 40, bottom: 80),
                        children: [
                          Padding(
                            padding: EdgeInsets.only(bottom: 20, top: 20),
                            child: Align(
                              alignment: Alignment.center,
                              child:
                                  Texts.headline3("Cart", themeModel.textColor),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(bottom: 20),
                            child: Align(
                              alignment: Alignment.center,
                              child: Texts.descriptiveItems(
                                  "All orders over R600 recieve free delivery",
                                  themeModel.textColor),
                            ),
                          ),

                          ///List of cart items
                          AnimatedSize(
                            vsync: this,
                            duration: Duration(milliseconds: 300),
                            child: Column(
                              children:
                                  List.generate(cartItems.length, (index) {
                                return FadeIn(
                                  child: Cards.cart(context,
                                      goToProduct: () {
                                        ProductDetails.create(
                                                context, products[index])
                                            .then((value) {
                                          if (value != null) {
                                            final homeModel =
                                                Provider.of<HomeModel>(context,
                                                    listen: false);

                                            homeModel.goToPage(0);

                                            showDialog(
                                              context: context,
                                              builder: (context) => Dialogs.success(
                                                  context,
                                                  message:
                                                      "Congratulations!\nYour order is placed!"),
                                            ).then((value) {
                                              widget.bloc.removeCart();
                                            });
                                          }
                                        });
                                      },
                                      cartItem: cartItems[index],
                                      updateQuantity:
                                          widget.bloc.updateQuantity,
                                      updateUnit: widget.bloc.updateUnit,
                                      delete: () async {
                                        await widget.bloc.removeFromCart(
                                            cartItems[index].reference);
                                      }),
                                );
                              }),
                            ),
                          ),

                          ///Checkout button
                          FadeIn(
                            duration: Duration(milliseconds: 400),
                            child: Buttons.button(
                                color: themeModel.accentColor,
                                widget: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Icon(
                                      Icons.arrow_forward,
                                      color: Colors.white,
                                    ),
                                    Padding(
                                      padding: EdgeInsets.only(left: 10),
                                      child: Texts.headline3(
                                          "Checkout", Colors.white),
                                    )
                                  ],
                                ),
                                onPressed: () async {
                                  final checkoutResult =
                                      await Checkout.create(context);
                                  if (checkoutResult ?? false) {
                                    final homeModel = Provider.of<HomeModel>(
                                        context,
                                        listen: false);

                                    homeModel.goToPage(0);

                                    showDialog(
                                      context: context,
                                      builder: (context) => Dialogs.success(
                                          context,
                                          message:
                                              "Congratulations!\nYour order is placed!"),
                                    ).then((value) {
                                      widget.bloc.removeCart();
                                    });
                                  }
                                }),
                          )
                        ],
                      );
                    }
                  } else if (snapshot.hasError) {
                    return FadeIn(
                      child: Center(
                        child: SvgPicture.asset(
                          'images/state_images/error.svg',
                          width: width * 0.5,
                          fit: BoxFit.cover,
                        ),
                      ),
                    );
                  } else {
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  }
                });
          }
        } else if (snapshot.hasError) {
          return FadeIn(
            child: Center(
              child: SvgPicture.asset(
                'images/state_images/error.svg',
                width: width * 0.5,
                fit: BoxFit.cover,
              ),
            ),
          );
        } else {
          return Center(
            child: CircularProgressIndicator(),
          );
        }
      },
    );
  }
}

CodePudding user response:

  • This error actually occurs when we use a null check operator (!) and the value on which it is used is null.
  • ! operator is used to signify that the variable on which it is used will never be null but in your case it might be having a null value.
  • The error might be occurring as snapshot.data is having a null value. Another reason might be that another file in your project is having a null value and ! operator is put on it. I think the problem can also be in Cards.dart file. So, check it too. And ensure that the value which you are receiving must not be null along with ! operator.
  • Related