简体   繁体   English

如何使用 flutter 将数据从一页传递到另一页?

[英]How to pass data from one page to another with flutter?

I figured already how to transfer data from one page to another but I found a challenge...我已经想好如何将数据从一个页面传输到另一个页面,但我发现了一个挑战......

  1. A user has to select one of the matches from pg1 (footballEvent) to go to page2(footballScreen)用户必须 select 从 pg1 (footballEvent) 到 go 到 page2(footballScreen) 的匹配项之一

footballEvent pg1足球赛事 pg1

  1. Images displayed on page2 should be the same as the ones previous selected from pg1, page2 上显示的图像应与之前从 pg1 中选择的图像相同,

footballScreen pg2足球画面 pg2

  1. This is the code I wrote to pass data from pg1 to pg2.这是我编写的将数据从 pg1 传递到 pg2 的代码。 [6]: https://i.stack.imgur.com/u2etq.png the problem with this approach is: leftPlayer[index] , because only the data on the left it's updated on pg2, but not the data on the right side. [6]: https://i.stack.imgur.com/u2etq.png这种方法的问题是: leftPlayer[index] ,因为只有左边的数据在pg2上更新,而不是右边的数据边。

  2. This is the code I used inside pg2 to update a team's image data.imageUrl.这是我在 pg2 中用于更新团队图像 data.imageUrl 的代码。 [5]: https://i.stack.imgur.com/PSkFf.png [5]: https://i.stack.imgur.com/PSkFf.png

  3. This is how I structured the matches: (and I believe this could be the problem because I'm not using the rightPlayer data on the code(3rd point) to pass data to the 2nd pg).这就是我构建比赛的方式:(我相信这可能是问题所在,因为我没有使用代码(第 3 点)上的 rightPlayer 数据将数据传递给第 2 pg)。 leftPlayer and rightPlayer leftPlayerrightPlayer

So, how can I fix this?那么,我该如何解决这个问题?

You need to pass a values to a next screen class constructor.您需要将值传递给下一个屏幕 class 构造函数。

Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => FootbalScreen(leftPlayer[index]),
  ),
);

RouteSettings useful when you defining routing on an app main class and all routing managed in this class.当您在应用程序主 class 上定义路由以及在此 class 中管理的所有路由时, RouteSettings很有用。 You can read more about routing organization in Documentation .您可以在文档中阅读有关路由组织的更多信息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM