3

Null check operator used on a null value in calendar! i am assigning calendar with ? mark then the this null operator error is occurring and when i am adding the late calendar LateInitializationError: Field '_calendar@94028380' has not been initialized error is getting

class MedicationListChild extends StatefulWidget { final String? medicationName; final String? medicationUID; final String? childUid; final String? childName; final Calendar? calendar; const MedicationListChild({ Key? key, this.medicationName, this.medicationUID, this.childUid, this.childName, this.calendar, }) : super(key: key); @override _MedicationListChildState createState() => _MedicationListChildState(); } class _MedicationListChildState extends State<MedicationListChild> { Calendar? _calendar; @override void initState() => super.initState(); @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.only(top: 7.0), child: Card( elevation: 3.0, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(7.0)), child: InkWell( splashColor: Colors.blue, highlightColor: Colors.green, onTap: () { Navigator.of(context).push( MaterialPageRoute( builder: (context) => CalendarEventPage(_calendar!), ), ); }, 

3 Answers 3

2

Instead of using _calendar use widget.calendar:

Navigator.of(context).push( MaterialPageRoute( builder: (context) => CalendarEventPage(widget.calendar!), ), ); 
Sign up to request clarification or add additional context in comments.

Comments

1

You didn't assign any value on _calendar, therefore it stays null.

Instead of using ! directly, it is better to do a null check 1st.

 onTap: () { if(_calendar!=null) { Navigator.of(context).push( MaterialPageRoute( builder: (context) => CalendarEventPage(_calendar), ), ); }, } 

If you just like access the widget variable do

 onTap: () { if(widget.calendar!=null) { Navigator.of(context).push( MaterialPageRoute( builder: (context) => CalendarEventPage(widget.calendar), ), ); }, } 

Comments

1

Try the following code:

class MedicationListChild extends StatefulWidget { final String? medicationName; final String? medicationUID; final String? childUid; final String? childName; final Calendar? calendar; const MedicationListChild({ Key? key, this.medicationName, this.medicationUID, this.childUid, this.childName, this.calendar, }) : super(key: key); @override _MedicationListChildState createState() => _MedicationListChildState(); } class _MedicationListChildState extends State<MedicationListChild> { @override void initState() => super.initState(); @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.only(top: 7.0), child: Card( elevation: 3.0, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(7.0)), child: InkWell( splashColor: Colors.blue, highlightColor: Colors.green, onTap: () { Navigator.of(context).push( MaterialPageRoute( builder: (context) => CalendarEventPage(widget.calendar!), ), ); }, 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.