我正在尝试通过Flutter中的构造函数传递参数。 当我在新页面上收到参数时,它为null。
我尝试更改
发送参数musicPlayerModel:
Align(
alignment: Alignment.bottomRight,
child: FlatButton(
child: Text('Settings'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SettingPage(musicPlayerModel: _musicPlayer,),
),
);
},
),
)
检索它,它为null:
class SettingPage extends StatefulWidget {
SettingPage({@required this.musicPlayerModel});
final MusicPlayerModel musicPlayerModel;
UserApi _userApi = UserApi();
UserModel _userModel = UserModel();
_SettingPageState createState() =>
_SettingPageState(musicPlayerModel, _userModel, _userApi);
}
_musicPlayer
在第一页的开头实例化。我还使用调试工具检查了该值是否不为空。
class _MainPageState extends State<MainPage> {
User user;
_MainPageState(userInfo) {
this.user = User(userInfo);
_musicPlayer = MusicPlayerModel(user);
}
在创建新页面方面,我可能缺少一些基本知识吗?
答案 0 :(得分:0)
尝试在MaterialPageRoute之外创建页面
Align(
alignment: Alignment.bottomRight,
child: FlatButton(
child: Text('Settings'),
onPressed: () {
final page = SettingPage(musicPlayerModel: _musicPlayer);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => page,
),
);
},
),
)