如何跨有状态小部件传递数据?

How to pass data across Stateful widget?

所以我的问题是我在第一个小部件中从 firebase 获取数据,然后我单击并通过 void -> 另一个有状态小部件打开一个 bottomsheet,我如何将快照数据从第一个小部件传递到另一个小部件?

以下代码无效...

....
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
        });
  }

新错误:位置参数太多:需要 0 个,但找到 1 个。

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 { 
    ...
    }

对于新出现的错误,只需删除大括号: 将 BottomSheetWidget({this.author}); 替换为 BottomSheetWidget(this.author);

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 { 
    ...
    }