使用带有 RaisingButton onPressed() 的异步函数将对象传递到另一个路由

Ayr*_*ing 4 dart flutter flutter-onpressed

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

Error: This expression has type 'void' and can't be used. 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'),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!

G g*_*ffo 7

用括号括起来,如下所示

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