In my flutter app I need to pass on data from a widget to another widget. Both are encapsulated in a Column widget, the diagram below best describes it
I've tried using inherited widget, but that doesn't seem to help, here's the code for the inherited widget I created:
import 'package:flutter/material.dart';
class updateMonth extends StatefulWidget {
final Widget child;
const updateMonth({Key? key,required this.child}) : super(key: key);
@override
_updateMonthState createState() => _updateMonthState();
}
class _updateMonthState extends State<updateMonth> {
int month=DateTime.now().month;
void updMonth(int newMonth){
setState(() =>
month=newMonth
);
}
@override
Widget build(BuildContext context)=>monthFromCal(
child: widget.child,
month: month,
newMonth: this,
);
}
class monthFromCal extends InheritedWidget {
final int month;
final _updateMonthState newMonth;
const monthFromCal({
Key? key,
required Widget child,
required this.month,
required this.newMonth
}) : super(key: key, child: child);
static _updateMonthState of(BuildContext context)=>context
.dependOnInheritedWidgetOfExactType<monthFromCal>()!.newMonth; //NULL CHECK
@override
bool updateShouldNotify(monthFromCal old) {
return old.month!=month;
}
}
updateMonth() is a stateful widget that is supposed to receive and update the value of month, which would then be passed on to the inherited widget monthFromCal(). When I run this, I get "null check operator used on a null value" error. For your ref, here are the two widgets:
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
TableCalendar(
focusedDay: _focusedDay, ///DateTime _focusedDay=DateTime.now();
onPageChanged: (focusedDay) {
_focusedDay = focusedDay;
setState(() {
monthFromCal.of(context).updMonth(_focusedDay.month);
});
},
),
Container(
height: MediaQuery.of(context).size.height*0.3,
width: MediaQuery.of(context).size.width,
child: monthEvents(month: DateTime.now().month,),
),
],
),
How can I solve this ? Is there any other way of passing the value ? Thanks in advance