简体   繁体   English

我如何将数据从一个小部件传递到 flutter 中的另一个小部件?

[英]How i can pass Data from one widget to another widget in flutter?

I want to pass Data from one widget to another widget.我想将数据从一个小部件传递到另一个小部件。 I'm getting data from firebase and I want the pass that data across the widgets.我从 firebase 获取数据,我希望通过小部件传递该数据。 How to pass Data from one screen to another screen.如何将数据从一个屏幕传递到另一个屏幕。 this is how my code looks like.这就是我的代码的样子。

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  int _currentindex = 0;
  final List<Widget> _children = [
    Dashboard(),
    Search(),

  ];

  void onTappedBar(int index) {
    setState(() {
      _currentindex = index;
    });
  }

  @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 return Scaffold(
      backgroundColor: Color(0xFFf6f5fb),
      body: _children[_currentindex],
      bottomNavigationBar: CurvedNavigationBar(
        onTap: onTappedBar,
        index: _currentindex,
        items: <Widget>[
          Icon(
            Icons.home,
          ),
          Icon(
            Icons.search,     
        ],
      ),
    );
        });
  }
}

From this widget, Data get loaded from firebase.从此小部件中,数据从 firebase 加载。 Now I want pass snapshot.data to Dashboard() widget and Search() widget.现在我想将snapshot.data传递给Dashboard()小部件和Search()小部件。

i want show the username Dashboard().我想显示用户名 Dashboard()。 this how my ```Dashboard()`` Widget for exmaple这就是我的 ```Dashboard()`` 小部件的例子

class Dashboard extends StatefulWidget {
  @override
  _DashboardState createState() => _DashboardState();
}

class _DashboardState extends State<Dashboard> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('Here i want display the data')
      ),
    );
  }
}

Here is the model class:这是 model class:

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


pass it through constructor if it is simple scenario like yours:如果它是像你这样的简单场景,则通过构造函数传递它:

class Dashboard extends StatefulWidget {
  final UserData userData;

  // userData is not optional-named parameter
  const Dashboard(this.userData, {Key key}) : super(key: key);
  @override
  _DashboardState createState() => _DashboardState();
}

class _DashboardState extends State<Dashboard> {
  @override
  Widget build(BuildContext context) {
    final UserData userData = widget.userData;//do something with user
    return Center(
        child: Text('user is ${user.username}')
    ),
    );
  }
}

and the HomeState (don't save widgets as fields of the class, create them on the fly):HomeState (不要将小部件保存为 class 的字段,即时创建它们):

class _HomeState extends State<Home> {
  int _currentindex = 0;

  void onTappedBar(int index) {
    setState(() {
      _currentindex = index;
    });
  }

  @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  Scaffold(
    backgroundColor: Color(0xFFf6f5fb),
    body: Column(
               children: [
                 Dashboard(userData),
                 Search(userData),
                 ],
    ),
    bottomNavigationBar: CurvedNavigationBar(
    onTap: onTappedBar,
    index: _currentindex,
    items: <Widget>[
    Icon(
    Icons.home,
    ),
    Icon(
    Icons.search,
    ],
    ),
    );
    });
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何将 QuerySnapshot 数据从一个小部件传递到 flutter 中的另一个小部件? - How i can pass QuerySnapshot data from one widget to another widget in flutter? 如何将检索到的数据从 firebase 放入 Flutter 中的小部件? - How can I put retrieved data from firebase to a widget in Flutter? 如何在 Flutter 中使用 Firebase firestore 中的数据填充我的 ExpansionTile 小部件 - How Can I fill my ExpansionTile widget with data from Firebase firestore in Flutter 如何在小部件上显示从 Firebase 到 Flutter 的当前用户数据 - How can I display on a widget the current user data from Firebase to Flutter 如何在 Flutter 中加载特定的小部件? - How can I load a particular widget in Flutter? 如何将另一个范围中的变量用于子小部件? - How can i use a variable from another ambit into a child Widget? 如何使用出现在另一个小部件中? - How can I use a appear in another widget? 如何将初始值传递给 Timer 小部件 - Flutter - How to pass an initial value to the Timer widget - Flutter 我如何在 FutureBuilder Widget 的构建器 function 中等待 - Flutter - How can I await inside the builder function of FutureBuilder Widget - Flutter 我如何以列表形式从云 Firestore 显示数据并传递到 flutter 中的另一个文件 - How i can display data in form of list from cloud firestore and pass to another file in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM