Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i pass data from a child widget to a parent widget

I have seen similar answers to my question, example is this How to pass data from child widget to its parent , but the data is passed through an onPressed event. How can i pass data from my child widget to the parent widget, without any onPressed or onTap event.

Code

  class ParentWidget extends StatelessWidget {
  String? passedData;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ChildWidget(),
          Text(passedData != null ? passedData! : 'No data passed'),
        ],
      ),
    );
  }
}

//How can i pass the title string to the ParentWidget as passedData String

class ChildWidget extends StatelessWidget {
  String title = 'Happy New Year';
  @override
  Widget build(BuildContext context) {
    return Center(
        child: Container(
      child: Text(title),
    ));
  }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use callback direct inside build method. To handle build error using setState need to wait a frame. I am using addPostFrameCallback.

class ParentWidget extends StatefulWidget {
  const ParentWidget({
    Key? key,
  }) : super(key: key);

  @override
  State<ParentWidget> createState() => _ParentWidgetState();
}

class _ParentWidgetState extends State<ParentWidget> {
  String? passedData;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ChildWidget(
            fallback: (p0) {
              WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
                setState(() {
                  passedData = p0;
                });
              });
            },
          ),
          Text(
            passedData == null ? 'No data passed' : passedData!,
          ),
        ],
      ),
    );
  }
}


class ChildWidget extends StatelessWidget {
  final String title = 'Happy New Year';
  final Function(String) fallback;

  const ChildWidget({
    Key? key,
    required this.fallback,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    fallback(title);
    return Center(
        child: Container(
      child: Text(title),
    ));
  }
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading