gpt4 book ai didi

flutter - 使用带有 RaisedButton onPressed() 的异步函数将对象传递到另一条路线

转载 作者:行者123 更新时间:2023-12-03 04:20:36 25 4
gpt4 key购买 nike

我希望能够在将对象作为参数传递时按下按钮导航到另一个屏幕。该对象是使用不同 dart 文件中的 getPlayer() 函数创建的;因此,异步功能。每当我运行代码时,我都会收到错误:

错误:此表达式的类型为“void”,无法使用。 onPressed: loadPlayerCard('nebula'),

代码如下:

    class _HomeState extends State<Home> {

List<Player> players;

void loadPlayerCard (String playerName) async {
Player player = await getPlayer(playerName);
players.add(player);

Navigator.pushNamed(context, '/playerCard', arguments: player);
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[900],
appBar: AppBar(
title: Text('Smash Tracker'),
centerTitle: true,
backgroundColor: Colors.grey[850],
elevation: 0,
),
body: Center(
child: RaisedButton.icon(
onPressed: loadPlayerCard('nebula'), //This is where the error message points to
icon: Icon(Icons.touch_app),
label: Text('Nebula'),
),
),
);
}
}

感谢任何帮助!

最佳答案

如下用括号括起来

RaisedButton.icon(
onPressed:() async { loadPlayerCard('nebula'); }, // Fix for the issue
icon: Icon(Icons.touch_app),
label: Text('Nebula'),
),

关于flutter - 使用带有 RaisedButton onPressed() 的异步函数将对象传递到另一条路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62845077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com