0

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

enter image description here

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

Ayush Nanglia
  • 89
  • 1
  • 3
  • 10

1 Answers1

0

What you are really looking for is a function not a widget here is the solution :

Declare the function somewhere

int updateMonth(int month) => month = DateTime.now().month;

& then just call it wherever you want - it will take data & return it as you're requesting

updateMonth(_focusedDay);

the only issue is - i don't really get what you're trying to calculate but this is the solution to your problem

Aristidios
  • 1,921
  • 2
  • 13
  • 24
  • So the variable `_focusedDay` is initialised to `DateTime.now()`, and it is re-assigned inside `onPageChanged()`. What I want is to pass the updated value of `_focusedDay` to the widget `monthEvents()` I'm not getting the expected result from your answer, can you please see that again ? – Ayush Nanglia Jul 18 '21 at 04:14