如何在屏幕之间传递数据?

我如何将数据从一个小部件传递到另一个小部件。 我正在从Firebase获取数据,并且希望在小部件之间传递数据。 我不能做到这一点。

例如,为此,我从此小部件获取数据

class _SettingState extends State<Setting> {
  final AuthService _auth = AuthService();
  @override
  Widget build(BuildContext context) {
    final user = Provider.of<User>(context);
    return StreamBuilder<UserData>(
        stream: DatabaseService(uid: user.uid).userData,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            UserData userData = snapshot.data;
            return Column(
              children: <Widget>[
                Container(
                 child: Text('working)
                )
               ]

            );
          } 
        });
  }
}

现在我想将数据传递到此小部件

class Inbox extends StatefulWidget {
  @override
  _InboxState createState() => _InboxState();
}

class _InboxState extends State<Inbox> {
  final AuthService _auth = AuthService();

  @override
  Widget build(BuildContext context) {
    return Center(
        child: Text('Data'),
      ),
    );
  }
}

这是模型课

class User {
  final String uid;

  User({this.uid});
}

class UserData {

  final String uid;
  final String username;
  final String phonenumber;


  UserData({ this.uid, this.username, this.phonenumber });


评论
  • 五脏俱全
    五脏俱全 回复

    You can add a parameter to the Inbox widget, and use it in the state like this:

    class Inbox extends StatefulWidget {
      final UserData userData;
    
      Inbox({Key key, this.userData}): super(key: key);
    
      @override
      _InboxState createState() => _InboxState();
    }
    
    class _InboxState extends State<Inbox> {
      final AuthService _auth = AuthService();
    
      @override
      Widget build(BuildContext context) {
        return Center(
            child: Text(widget.userData.username),
          ),
        );
      }
    }