【问题标题】:The getter 'loadingStatus' was called on null flutter吸气剂'loadingStatus'在空颤时被调用
【发布时间】:2020-10-30 05:05:46
【问题描述】:

我在颤振中遇到错误

它只是从提供者那里获取加载状态我在我使用的所有提供者中都有相同的错误

getter 'loadingStatus' 在 null Flutter 上被调用

此错误仅出现在控制台中,但应用运行正常 -_-

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;

}

【问题讨论】:

    标签: flutter dart dart-pub flutter-test


    【解决方案1】:

    WidgetsBinding.instance.addPostFrameCallback 将在一个帧之后被调用(作为下一帧的 Future),因为它构建 _generalService.loadingStatus _generalService 的第一帧还没有被引用(你正在调用null.loadingStatus)。如果您想保留该逻辑,只需将其更改为_generalService?.loadingStatus != null &amp;&amp; _generalService.loadingStatus

    我不知道您的课程,但也许将GeneralService 改编为ProfileServiceProxyProviderFutureProvider_profileService.getReviews(context) 会更好,但这是您面临的问题的一部分

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 2021-08-05
      • 2021-04-02
      • 2021-03-24
      • 2020-10-28
      • 1970-01-01
      • 2018-11-16
      相关资源
      最近更新 更多