0

So my question is i get data from firebase in first widget, then i click and open a bottomsheet through void -> another stateful widget, how can i pass the snapshot data from first widget to the other one?

Below code is not working...

....
Widget build(BuildContext context) {
          return Container(
              ElevatedButton(
                    child: Text('Request a tour'),
                    onPressed: () {
                      displayBottomSheet(context, widget.snapshot.data()["author"]);
                    },
                  ),
                );


    void displayBottomSheet(BuildContext context, String author) {  //updated
    showModalBottomSheet(
        context: context,
        builder: (ctx) {
          return BottomSheetWidget(author);  //updated
        });
  }

NEW ERROR: Too many positional arguments: 0 expected, but 1 found.

class BottomSheetWidget extends StatefulWidget {   

     final String author;                 //updated
     BottomSheetWidget({this.author});    //updated

     @override
  class _BottomSheetWidgetState createState() => _BottomSheetWidgetState();
}

class _BottomSheetWidgetState extends State<BottomSheetWidget> {

    Widget build(BuildContext context) {
          return Container(
                  new ElevatedButton(
                  child: Text('Send'),
                  onPressed: () {
                    requestTour(widget.author);     //updated
                  },
                ),
               .....
              }


    requestTour(String userName) async { 
    ...
    }
icantcode
  • 142
  • 1
  • 15

2 Answers2

1

Just remove curly braces for new arrived error: replace BottomSheetWidget({this.author}); with BottomSheetWidget(this.author);

  • thanks! it worked!, may i know why curly is not required? – icantcode Apr 29 '21 at 16:38
  • 1
    Sure, there are multiple types of ways we can pass data through parameters in dart. 1. Default (without any braces), 2. Named (as you have defined - with curly braces) 3. optional. so if you using curly braces...you need to add a name when you are passing the data. (in this example: `BottomSheetWidget(author: author);`). Like all other widgets, we are using. Hope this gives you a basic understanding. you can refer to this article for more info. https://dart.dev/guides/language/language-tour#parameters – Pruthvi Patel Apr 29 '21 at 16:46
1
class BottomSheetWidget extends StatefulWidget {   

     final String author;                 //updated
     BottomSheetWidget(this.author);    //<-- remove {}

     @override
  class _BottomSheetWidgetState createState() => _BottomSheetWidgetState();
}

class _BottomSheetWidgetState extends State<BottomSheetWidget> {

    Widget build(BuildContext context) {
          return Container(
                  new ElevatedButton(
                  child: Text('Send'),
                  onPressed: () {
                    requestTour(widget.author);     //updated
                  },
                ),
               .....
              }


    requestTour(String userName) async { 
    ...
    }
Younss AIT MOU
  • 871
  • 7
  • 14