getter 'loadingStatus' 被调用为 null flutter

The getter 'loadingStatus' was called on null flutter

我在 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()
);
  }
}

我尝试了越来越多,但没有任何改变所以请蚂蚁告诉我错误在哪里?

这是提供商代码

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

我不知道你的 类 但也许将 GeneralService 改编为 ProfileServiceProxyProviderFutureProvider 会更好18=],但这是您面临的问题的一部分