空值检查运算符用于 flutter 的空值
Null check operator used on a null value for flutter
所以我正在将一个项目切换到 flutter null safety 但我得到了 Null check operator used on a null value error 我尝试查看其他类似的 尝试过但它对我不起作用完整的错误是如下
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
等等,这是在多个地方发生的,我不知道如何解决这个问题。
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()
提到的另一行
Init? _initData;
City get selectedCity {
return _initData!.cities
.firstWhere((element) => element.id == _initData!.selectedCityId);
}
Init get initData => _initData!;// home_viewmodel.dart 25
在第 30 行的 home_banner
中,您正在使用 initData
方法,该方法 return 是一个不可为 null 的值。
因此创建一个可为空的 return 类型的 initData
并从 _initData
中删除 !
。
Init? get initData => _initData;// home_viewmodel.dart 25
并且如果您在代码中使用 get selectedCity
而没有检查 _initData
的空值,这可能会导致您的代码出错,
所以在此方法中也添加一个空检查。
City get selectedCity {
City? city = _initData?.cities
.firstWhere((element) => element.id == _initData?.selectedCityId);
if(city==null){
return City();// return default city
}else{
return city;
}
}
所以我正在将一个项目切换到 flutter null safety 但我得到了 Null check operator used on a null value error 我尝试查看其他类似的
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
等等,这是在多个地方发生的,我不知道如何解决这个问题。
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()
提到的另一行
Init? _initData;
City get selectedCity {
return _initData!.cities
.firstWhere((element) => element.id == _initData!.selectedCityId);
}
Init get initData => _initData!;// home_viewmodel.dart 25
在第 30 行的 home_banner
中,您正在使用 initData
方法,该方法 return 是一个不可为 null 的值。
因此创建一个可为空的 return 类型的 initData
并从 _initData
中删除 !
。
Init? get initData => _initData;// home_viewmodel.dart 25
并且如果您在代码中使用 get selectedCity
而没有检查 _initData
的空值,这可能会导致您的代码出错,
所以在此方法中也添加一个空检查。
City get selectedCity {
City? city = _initData?.cities
.firstWhere((element) => element.id == _initData?.selectedCityId);
if(city==null){
return City();// return default city
}else{
return city;
}
}