在 null 上调用了 getter 'calendarFormat'

问题描述

我创建了一个 TableCalendar,它从本地数据库 (Sembast) 加载某些事件。因此我使用了 Riverpod 架构。但是每当我热重载时,我的应用程序并没有真正崩溃,而是出现此错误。它仍然可以很好地加载应用程序。

======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building Consumer(dirty,dependencies: [UncontrolledProviderScope],state: _ConsumerState#9c8ef):
The getter 'calendarFormat' was called on null.
Receiver: null
Tried calling: calendarFormat

The relevant error-causing widget was: 
  Consumer file:///Users/cengizbayram/Desktop/Progba/footstars/lib/ui/pages/homepage.dart:32:15
When the exception was thrown,this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      HomePage.getTableCalendar (package:footstars/ui/pages/homepage.dart:75:47)
#2      HomePage.build.<anonymous closure> (package:footstars/ui/pages/homepage.dart:44:27)
#3      Consumer.build (package:Flutter_riverpod/src/consumer.dart:148:20)
#4      _ConsumerState.build (package:Flutter_riverpod/src/consumer.dart:300:35)
...

我相信错误来自我实例化我的 CalendarSettings 类的方式,它基本上是一个包含 bool 和 CalendarFormat 的类。我只是不知道如何调用它来停止收到错误消息。我已经将weeklyForm 的Initialization 放入addPostFrameCallback 方法中,bc 当它在外面时,热重载会导致我的应用程序崩溃。

 class HomePage extends StatelessWidget{
  CalendarSettings weeklyForm;
  TextStyle dayStyle(FontWeight fontWeight){
    return TextStyle(color: Color(0xff30384c),fontWeight: fontWeight);
  }
  CalendarController _controller = CalendarController();

  HomePage();
  final AuthService _auth = AuthService();
  @override
  Widget build(BuildContext context) {
    WidgetsBinding.instance?.addPostFrameCallback((_) {
      weeklyForm = CalendarSettings(false,CalendarFormat.week);
      context.read(calendarNotifierProvider).fetchEvents();
    });
    return Scaffold(
      body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,children: <Widget>[
              Consumer(
                builder: (context,watch,child){
                  final state = watch(calendarNotifierProvider.state);
                  if (state is CalendarInitial){
                    return Text("Warte");
                  }
                  else if (state is CalendarLoading){
                    return Text("Calendar lädt");
                  } else if (state is CalendarLoaded){
                    return InkWell(child: Container(
                      child: Column(
                        children: [
                          getTableCalendar(weeklyForm,state.eventMap),],),onTap: (){
                        Navigator.of(context)?.pushNamed(RouteGenerator.calendarPage);
                      },);
                  } else {
                    print("aber warum aldöööö");
                    return Text("FEHLA");
                  }
                },RaisedButton(child: Text('DELETE ALL'),onpressed: (){
                  context.read(calendarNotifierProvider).deleteall();
                },RaisedButton(child: Text('WEEKLY CHALLENGE'),onpressed: (){
                  Navigator.of(context)?.pushNamed(RouteGenerator.weeklyChallengePage);
                },);
  }

  TableCalendar getTableCalendar (CalendarSettings calendarSettings,Map<DateTime,List<dynamic>> eventMap){
    return  TableCalendar(
      availableGestures: null,initialCalendarFormat: calendarSettings.calendarFormat,events: eventMap,calendarController: _controller,startingDayOfWeek: StartingDayOfWeek.monday,calendarStyle: CalendarStyle(
        weekdayStyle: dayStyle(FontWeight.normal),weekendStyle: dayStyle(FontWeight.normal),selectedColor: Colors.lightBlue,todayColor: Colors.lightBlueAccent,daysOfWeekStyle: DaysOfWeekStyle(
          weekdayStyle: TextStyle(
              color: Color(0xff30384c),fontWeight: FontWeight.bold,fontSize: 16
          ),weekendStyle: TextStyle(
            color: Color(0xff30384c),fontSize: 16,)
      ),headerVisible: calendarSettings.header,headerStyle: HeaderStyle(
          formatButtonVisible: false,titleTextStyle: TextStyle(
            color: Color(0xff30384c),fontSize: 20,leftChevronIcon: Icon(
            Icons.chevron_left,color: Color(0xff30384c),rightChevronIcon: Icon(
            Icons.chevron_right,);
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)