gpt4 book ai didi

flutter - 在空 flutter 上调用 setter/getter 'loadingStatus'

转载 作者:行者123 更新时间:2023-12-03 04:20:40 24 4
gpt4 key购买 nike


我在 flutter 中有错误
它只是从提供程序获取加载状态,在我使用的所有提供程序中都有相同的错误

The getter 'loadingStatus' was called on null flutter


This error appears only in console but app works fine -_-

class _DoctorInfoPageState extends State<DoctorInfoPage> {
GeneralService _generalService;
ProfileService _profileService;
@override
void initState() {
// TODO: implement initState
super.initState();

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_generalService = Provider.of<GeneralService>(context);
_profileService = Provider.of<ProfileService>(context);
loadingReviews();
});
}

loadingReviews() async {
_generalService.setLoadingState(true);
await _profileService.getReviews(context);
_generalService.setLoadingState(false);
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
elevation: 0.0,
centerTitle: true,
backgroundColor: Colors.white,
leading: IconButton(
icon:
Icon(Icons.chevron_left, size: 30, color: Const.appMainBlueColor),
onPressed: () {
print('test');
},
),
title: Text(
"Doctor Info",
style: TextStyle(
color: Const.appMainBlueColor,
fontWeight: FontWeight.w600,
fontSize: 18),
),
),
body: _generalService.loadingStatus != null &&
_generalService.loadingStatus
? Center(child: PumpHeart(size: 35.0))
: SafeArea()
);
}
}
我尝试了越来越多,但是什么也没有改变,所以一个 Ant 可以告诉我错误在哪里吗?
这是提供者代码
class GeneralService with ChangeNotifier {
bool _isLoading = false;

// Change Loading Status
void setLoadingState(bool value) {
_isLoading = value;
notifyListeners();
}

// Get Loading Status
bool get loadingStatus => _isLoading;

}

最佳答案

WidgetsBinding.instance.addPostFrameCallback将在以后的一帧中被调用(作为下一帧的Future),因为构建_generalService.loadingStatus _generalService的第一帧尚未被引用(您正在校准null.loadingStatus)。如果您想保留该逻辑,只需将其更改为_generalService?.loadingStatus != null && _generalService.loadingStatus我不知道您的类(class),但将GeneralService改成ProxyProviderProfileServiceFutureProvider_profileService.getReviews(context)也许会更好,但这是您面临的问题的一部分

关于flutter - 在空 flutter 上调用 setter/getter 'loadingStatus',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62817635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com