使用路由并从子窗口小部件传递值

我使用路由在小部件和问题之间导航,如您所见,我有EditPhoto类,它需要id,但在main.dart中没有id。我想从另一个小部件中获得ID,无论如何有做吗?

main.dart

routes: {
          '/': (context) => HomePage(),'/login': (context) => Login(),'/edit_photo':(context) => EditPhoto(),}

照相课

class EditPhoto {

final String id;

EditPhoto(this.id)

}
cry1018 回答:使用路由并从子窗口小部件传递值

这是文档的一部分。 https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments

第1步:创建一个传递参数的类。在您的情况下,它只是一个id,但创建一个类,以便将来可以使用它传递其他一些参数。

class EditPhotoArgs {
   final String id;
   EditPhotoArgs(this.id);
}

第2步:单击“按钮”,您可以导航到此屏幕并传递参数。

Navigator.of(context).push(
      MaterialPageRoute(
        builder: (context) => EditPhoto(),// Pass the arguments as part of the RouteSettings. The
        // ExtractArgumentScreen reads the arguments from these
        // settings.
        settings: RouteSettings(
          arguments: EditPhotoArgs('id'),),);

第3步:从EditPhoto屏幕的Args中获取ID

// inside EditPhoto
final EditPhotoArgs args = ModalRoute.of(context).settings.arguments;
print(args.id); // prints the ID passed from the previous screen
本文链接:https://www.f2er.com/3140570.html

大家都在问