Flutter null check operator used on a null value?

I work on a shop app for learning.
And when I try to get data from the API, I always have this problem:

Null check operator used on a null value

And no matter what I do I keep having this error.

Here are my code blocks:

Main File:

void main() async
{
  //this line of code ensures that all the data are set and get before running the app
  WidgetsFlutterBinding.ensureInitialized();

  Bloc.observer = MyBlocObserver();
  DioHelper.init();
  await CacheHelper.init();

  Widget? widget;
  bool? isDarkTheme = CacheHelper.getData(key: 'isDark') ?? false;
  bool? onBoarding = CacheHelper.getData(key: 'onBoarding') ?? false;
  token = CacheHelper.getData(key: 'token')??'';
  print(token);

  if(onBoarding != null && onBoarding)
  {
    widget = token.isNotEmpty? ShopLayout() : ShopLoginScreen();
  }
  else {
    widget = OnBoardingScreen();
  }

  runApp(MyApp(isDarkTheme: isDarkTheme ,startWidget: widget));
}

class MyApp extends StatelessWidget
{
  final bool? isDarkTheme;
  final Widget? startWidget;

  MyApp({@required this.isDarkTheme, @required this.startWidget});
  @override
  Widget build(BuildContext context) {
      return MultiBlocProvider(
        providers:
        [
          BlocProvider(create: (BuildContext context) => ShopCubit()..changeAppMode(isDarkFromPreferences: isDarkTheme)),
          BlocProvider(create: (BuildContext context) => ShopCubit()..getHomeData()),
          BlocProvider(create: (BuildContext context) => WhatsappCubit()),
        ],
        child: BlocConsumer<ShopCubit, ShopStates>(
          listener: (context, state){},
          builder: (context, state) => MaterialApp
          (
            theme: lightTheme,
            darkTheme: darkTheme,
            themeMode: ThemeMode.light,//isDarkTheme!? ThemeMode.dark: ThemeMode.dark,
            debugShowCheckedModeBanner: false,
            home: startWidget,
          ),
        ),
      );
  }

}

Cubit File:

class ShopCubit extends Cubit<ShopStates>
{
  ShopCubit() : super(ShopInitialState());

  static ShopCubit get(context) => BlocProvider.of(context);

  int currentIndex = 0;

  bool isDark = true;
  ThemeMode? appTheme;

  List<Widget> screens =
  [
    ProductsScreen(),
    CategoriesScreen(),
    FavouritesScreen(),
    SettingsScreen(),
  ];

  void changeBottomSheetIndex(int index)
  {
    currentIndex = index;
    emit(ShopChangeBottomNavCurrentIndexState());
  }

  HomeModel? homeModel;
  void getHomeData()
  {
    emit(ShopLoadingHomeDataState());

    DioHelper.getData(path: HOME, token: token).then((value) {
      if (value.data != null) {

        homeModel = HomeModel.fromJson(value.data);
        print('Data fetched successfully');
        printFullText(homeModel!.data!.banners[0].image);
        emit(ShopSuccessHomeDataState());
      }
      else
      {
        emit(ShopErrorHomeDataState('Response data is null'));
      }
    }).catchError((error) {
      emit(ShopErrorHomeDataState(error.toString()));
      print('Error fetching data: ${error.toString()}');
    });

  }

  void changeAppMode({bool? isDarkFromPreferences})
  {
    if(isDarkFromPreferences != null)
    {
      isDark = isDarkFromPreferences;
      emit(ShopChangeAppModeState());
    }
    else
    {
      isDark = !isDark;
    }

        if(isDark)
        {
          appTheme = ThemeMode.dark;
          CacheHelper.saveData(key: 'isDark', value: isDark).then((value)
          {
            emit(ShopChangeAppModeState());
          }).catchError((error)
          {
            print('An error occurred while trying to set a new value as a shared preference');
          });
        } else
        {
          appTheme = ThemeMode.light;
          CacheHelper.saveData(key: 'isDark', value: isDark).then((value)
          {
            emit(ShopChangeAppModeState());
          }).catchError((error)
          {
            print('An error occurred while trying to set a new value as a shared preference');
          });
        }
  }
}

Dio Helper File:

class DioHelper {
  static Dio? dio;

  static void init() {
    dio = Dio(
      BaseOptions(
        baseUrl: 'https://student.valuxapps.com/api/',
        receiveDataWhenStatusError: true,
      ),
    );
  }

  static Future<Response> getData({
    @required String? path,
    Map<String, dynamic>? query,
    String lang = 'en',
    String? token,
  })
  async
  {
    dio!.options.headers = {
      'Content-Type':'application/json',
      'lang':lang,
      'Authorization':token??'',
    };

    return await dio!.get(path!, queryParameters: query!);
  }

  static Future<Response> postData({
    @required String? url,
    Map<String, dynamic>? query,
    @required Map<String, dynamic>? data,
    String lang = 'en',
    String? token,
  }) async
  {
    dio!.options.headers = {
      'Content-Type':'application/json',
      'lang':lang,
      'Authorization':token??'',
    };
    return dio!.post(url!, queryParameters: query, data: data);
  }
}

Cache Helper File:

class CacheHelper
{
  static SharedPreferences? sharedPreferences;

  static Future<void> init() async
  {
    sharedPreferences = await SharedPreferences.getInstance();
  }

  static dynamic getData({@required String? key})
  {
    if(sharedPreferences == null) {
      return 'error this value is null';
    }
    return sharedPreferences!.get(key!);
  }

  static Future<bool> saveData({
    @required String? key,
    @required dynamic value,
  }) async
  {
    if(value is String) return await sharedPreferences!.setString(key!, value);
    if(value is int) return await sharedPreferences!.setInt(key!, value);
    if(value is bool) return await sharedPreferences!.setBool(key!, value);
    return await sharedPreferences!.setDouble(key!, value);
  }

  static Future<bool> removeData({@required String? key}) async
  {
    return await sharedPreferences!.remove(key!);
  }
}

Data Receive File:

class HomeModel
{
    bool? status;
    HomeDataModel? data;

    HomeModel.fromJson(Map<String, dynamic> json)
    {
      status = json['status'];
      data = HomeDataModel.fromJson(json['data']);
    }
}

class HomeDataModel
{
  List<BannerModel> banners = [];
  List<ProductModel> products = [];

  HomeDataModel.fromJson(Map<String, dynamic> json)
  {
    json['banners'].forEach((element)
    {
      banners.add(element);
    });

    json['products'].forEach((element)
    {
      products.add(element);
    });
  }
}

class BannerModel
{
  int? id;
  String? image;

  BannerModel.fromJson(Map<String, dynamic> json)
  {
    id = json['id'];
    image = json['image'];
  }
}

class ProductModel
{
  int? id;
  dynamic price;
  dynamic oldPrice;
  dynamic discount;
  String? image;
  String? name;
  bool inFavourites = false;
  bool inCart = false;

  ProductModel.fromJson(Map<String, dynamic> json)
  {
    id = json['id'];
    price = json['price'];
    oldPrice = json['old_price'];
    discount = json['discount'];
    image = json['image'];
    name = json['name'];
    inFavourites = json['in_favourites'];
    inCart = json['in_cart'];
  }
}

note

After making a debug I found that the error occurs at the getHomeData method in the Cubit File,
but couldn’t find any Solutions.

New contributor

Omar Nasr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

I guess the error comes from this line:

printFullText(homeModel!.data!.banners[0].image);

Those exclamation marks there (called “non-null assertion operators”) mean that you expect homeModel not to be null and homeModel.data not to be null. That’s too much to expect, because these values come from parsing an HTTP response.

Possible solutions:

  1. Check homeModel and homeModel.data before using them, and if they are null react properly.
  2. Place everything you do with those values in a try-catch and react properly when the NoSuchMethodError is raised.

In both cases, remove the ! operators.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật