Flutter将用户实例传递到另一个状态并保持更新

问题描述 投票:0回答:1

我正在尝试进行登录/注册过程并将用户重定向到仪表板视图。

所以我有这个方法:

Future _redirectToDashboard() async {
    var route = new MaterialPageRoute(
      builder: (BuildContext context) => new Dashboard(userID: userID),
    );
    Navigator.of(context).push(route);
  }

然后在login()完成后调用它。

_register().whenComplete(() => _redirectToDashboard());

然后在Dashboard我这样做:

class Dashboard extends StatefulWidget
{
  int userID;
  Dashboard({Key key, this.userID}) : super(key: key);
  @override
  _Dashboard createState() => _DashboardState();
}

并使用它:

Text('User ID: ${widget.userID}')

但是我在Dashboard课堂上不能这样做:

Dashboard({Key key, this.userID, this.name, this.balance}) : super(key: key);

即使我在上面的函数中设置它:

builder: (BuildContext context) => new Dashboard(userID: userID, name: name, balance: balance), 

但它不起作用。我得到一个错误,例如name没有定义为什么我只能传递一个参数?有没有办法在每次用户打开此状态时如何使用新数据更新Dashboard

我读到我应该创建一个包含所有属性的单独的qazxsw poi类。但是如何将其应用到仪表板?

dart flutter
1个回答
1
投票

我假设你只添加了构造函数参数,而没有添加实际的类成员。

User
© www.soinside.com 2019 - 2024. All rights reserved.