skip to Main Content

I am getting the error:

Unhandled Exception: LateInitializationError: Field '_deviceCalendarPlugin@332335403' has not been initialized.late DeviceCalendarPlugin _deviceCalendarPlugin

This statement getting error I am adding ? sign but in console the error is showing null string is not supporting.
I am creating doctor appointment event

class DoctorCalendarsPage extends StatefulWidget {
  const DoctorCalendarsPage({Key? key, required String calendarName, required void Function() opendrawer, required double animationtime})
      : super(key: key);

  @override
  _DoctorCalendarsPageState createState() {
    return _DoctorCalendarsPageState();
  }
}

class _DoctorCalendarsPageState extends State<DoctorCalendarsPage> {
  late DeviceCalendarPlugin _deviceCalendarPlugin;
  List<Calendar> _calendar = [];
  List<Calendar> get _writableCalendars =>
      _calendar.where((c) => c.isReadOnly == false).toList();

  List<Calendar> get _readOnlyCalendars =>
      _calendar.where((c) => c.isReadOnly == true).toList();

  _CalendarsPageState() {
    _deviceCalendarPlugin = DeviceCalendarPlugin();
  }

  @override
  void initState() {
    super.initState();
    _retrieveCalendars();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            flex: 1,
            child: ListView.builder(
              itemCount: _calendar.length,
              itemBuilder: (BuildContext context, int index) {
                return GestureDetector(
                  key: Key(_calendar[index].isReadOnly == true
                      ? 'readOnlyCalendar${_readOnlyCalendars.indexWhere((c) => c.id == _calendar[index].id)}'
                      : 'writableCalendar${_writableCalendars.indexWhere((c) => c.id == _calendar[index].id)}'),
                  onTap: () async {
                    await Navigator.push(context,
                        MaterialPageRoute(builder: (BuildContext context) {
                      return DoctorEventsPage(_calendar[index],
                          key: const Key('calendarEventsPage'));
                    }));
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: Container(
                      height: 22,
                      child: Row(
                        children: [
                          Expanded(
                            flex: 1,
                            child: Text(
                              _calendar[index].name!,
                              style: Theme.of(context).textTheme.subtitle1,
                            ),
                          ),
                          Container(
                            width: 15,
                            height: 15,
                            decoration: BoxDecoration(
                                shape: BoxShape.circle,
                                color: Color(_calendar[index].color!)),
                          ),
                          const SizedBox(width: 10),
                          // Container(
                          //   margin: const EdgeInsets.fromLTRB(0, 0, 5.0, 0),
                          //   padding: const EdgeInsets.all(3.0),
                          //   decoration: BoxDecoration(
                          //       border: Border.all(color: Colors.blueAccent)),
                          //   // child: const Text('Default'),
                          // ),
                          Icon(_calendar[index].isReadOnly == true
                              ? Icons.lock
                              : Icons.lock_open)
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          final createCalendar = await Navigator.push(context,
              MaterialPageRoute(builder: (BuildContext context) {
            return DoctorCalendarAddPage();
          }));

          if (createCalendar == true) {
            _retrieveCalendars();
          }
        },
        child: const Icon(Icons.add),
        backgroundColor: Colors.green,
      ),
    );
  }

  void _retrieveCalendars() async {
    try {
      var permissionsGranted = await _deviceCalendarPlugin.hasPermissions();
      if (permissionsGranted.isSuccess &&
          (permissionsGranted.data == null ||
              permissionsGranted.data == false)) {
        permissionsGranted = await _deviceCalendarPlugin.requestPermissions();
        if (!permissionsGranted.isSuccess ||
            permissionsGranted.data == null ||
            permissionsGranted.data == false) {
          return;
        }
      }

      final calendarsResult = await _deviceCalendarPlugin.retrieveCalendars();
      setState(() {
        _calendar = calendarsResult.data as List<Calendar>;
      });
    } on PlatformException catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
  }
}

3

Answers


  1. Add _CalendarsPageState in your init state

    @override
    void initState() {
     super.initState();
      _retrieveCalendars();
      _CalendarsPageState();
    }
    
    Login or Signup to reply.
  2. The following mean the variable will never be null.. so u have to assign it first before using it

    • late DeviceCalendarPlugin _deviceCalendarPlugin;

    —————————–

    You can use the following way.. so until u have get the data u want to assign the variable will be null

    • DeviceCalendarPlugin? _deviceCalendarPlugin;
    Login or Signup to reply.
  3. Try initializing the instance first before calling it’s function. Add _deviceCalendarPlugin = DeviceCalendarPlugin(); or _CalendarsPageState() prior to calling anything related to its function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search