Null check operator used on a null value for flutter
In home_banner
at line 30, you are using initData
method which returns a not nullable value.
So make a nullable return type of initData
and remove !
from _initData
.
Init? get initData => _initData;// home_viewmodel.dart 25
And if you are using get selectedCity
in your code without checking the null value of _initData
, this may cause an error in your code,
So add a null check in this method also.
City get selectedCity {
City? city = _initData?.cities
.firstWhere((element) => element.id == _initData?.selectedCityId);
if(city==null){
return City();// return default city
}else{
return city;
}
}
Neelay Srivastava
I am doing my B.tech currently And I love doing android.I made this https://play.google.com/store/apps/details?id=com.unnionapp.mediaplayer
Updated on December 29, 2022Comments
-
Neelay Srivastava over 1 year
So I am switching a project to flutter null safety but i am getting Null check operator used on a null value error i tried looking into other similar issue tried but its not working for me the complete error is as following
The following _CastError was thrown building HomeBanner(dirty, dependencies: [MediaQuery, _InheritedProviderScope<HomeViewModel>], state: _HomeBannerState#2de56): Null check operator used on a null value The relevant error-causing widget was: HomeBanner file:///D:/fabricoMAt/lib/ui/views/home_screen.dart:26:17 When the exception was thrown, this was the stack: #0 HomeViewModel.initData (package:knoty/business_logic/view_models/home_viewmodel.dart:25:33) #1 _HomeBannerState._getImageSliders (package:knoty/ui/widgets/home_banner.dart:30:18) #2 _HomeBannerState.build (package:knoty/ui/widgets/home_banner.dart:20:23) #3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27) #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15) ... ==================================================================================================== ======== Exception caught by widgets library ======================================================= Null check operator used on a null value The relevant error-causing widget was: WhomeServices file:///D:/fabricoMAt/lib/ui/views/home_screen.dart:43:17
And so onn this is coming at multiple place and i am not getting any idea how to fix this.
HomeViewModel model = Provider.of<HomeViewModel>(context); Size screenSize = MediaQuery.of(context).size; return model.initData == null //home_banner line 30 ? [1, 2, 3, 4] .map( (item) => Shimmer.fromColors( child: Container( margin: EdgeInsets.only(right: 8), width: screenSize.width * .7, height: screenSize.width * .35, decoration: BoxDecoration( borderRadius: BorderRadius.all( Radius.circular(8.0), ), color: Colors.black, ), ), baseColor: Colors.grey.shade300, highlightColor: Colors.grey.shade100, ), ) .toList()
the other mentioned line
Init? _initData; City get selectedCity { return _initData!.cities .firstWhere((element) => element.id == _initData!.selectedCityId); } Init get initData => _initData!;// home_viewmodel.dart 25
-
Ravi Sevta about 3 yearsplease add some code of the file where the stack trace is pointing, just to understand the issue.
-
Neelay Srivastava about 3 years@RaviSevta i have added the lines that is pointed
-
iDecode about 3 yearsDoes this answer your question? Flutter 2.0.2 Null check operator used on a null value
-