flutter 我想发送我的模型类对象到下一个屏幕之前我这样做首先我在第二个屏幕中声明对象

mepcadol  于 2023-05-08  发布在  Flutter
关注(0)|答案(3)|浏览(152)

你好,我想把我的模型类对象发送到下一个屏幕以前我这样做第一我在第二个屏幕上声明对象,我想导航

class ContactsDetail extends StatelessWidget {
      ContactModel contactDetail;
      ContactsDetail({Key? key, required this.contactDetail}) : super(key: key);
    
    Then in navigator i passed the argument  
    
        onTap: () async {
                
                    Navigator.of(context).push(MaterialPageRoute<void>(builder: (BuildContext context) {
                        return ContactsDetail(
                          contactDetail: contactlist[index],);
                      },
                    ));
                  },`enter code here`

这工作完美,现在我正在使用名称路由,我想发送当前索引数据的整个模型,就像我上面做的那样,但我无法做到这一点现在这里是我的路由。

Route<dynamic> generateRoute(RouteSettings settings){
switch(settings.name)
{
  case LoginScreen.routeName:
  return MaterialPageRoute(builder: (context) => const LoginScreen());
  case TourDetailScreen.routeName:
    return MaterialPageRoute(builder: (context) => TourDetailScreen());

  default:
    return errorRoute();
}
}

And here is the class to which i want to send data

class TourDetailScreen extends StatelessWidget {
  static const routeName = 'tourist_detail_screen';
  TourModel tourDetails;
  TourDetailScreen({Key? key,required this.tourDetails}) : super(key: key);

and this is main 
MaterialApp(
        debugShowCheckedModeBanner: false,

        initialRoute: TouristHomeScreen.routeName,
        onGenerateRoute: (settings) => generateRoute(settings),
      ),
8wtpewkr

8wtpewkr1#

有几种方法可以做到这一点,但这里是我如何发送我的自定义模型的对象到下一个屏幕
首先在genrateRoute中声明一个变量作为你想要发送的模型

Route<dynamic> generateRoute(RouteSettings settings){
var tourModel = settings.arguments as TourModel;

在你的案例中,把上面的变量作为参数传递

case TourDetailScreen.routeName:
  return MaterialPageRoute(builder: (context) => TourDetailScreen(arguments : tourModel));

从你想导航的地方,像这样导航

Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context){
          return TourDetailScreen(
            arguments: toursList[index],
          );
        })

最后在你想导航的屏幕上

TourModel arguments;
TourDetailScreen({Key? key,required this.arguments}) : super(key: key);

和use可以在构建内部的任何地方使用参数,例如Text(arguments.yourFields)

r7knjye2

r7knjye22#

您也可以使用Navigator.pushNamed()方法的arguments参数在命名路由中传递数据。

Navigator.pushNamed(
      context,
      ExtractArgumentsScreen.routeName,
      arguments: ScreenArguments(
        'Extract Arguments Screen',
        'This message is extracted in the build method.',
      ),
    );

// A Widget that extracts the necessary arguments from
// the ModalRoute.
class ExtractArgumentsScreen extends StatelessWidget {
  const ExtractArgumentsScreen({super.key});

  static const routeName = '/extractArguments';

  @override
  Widget build(BuildContext context) {
    // Extract the arguments from the current ModalRoute
    // settings and cast them as ScreenArguments.
    final args = ModalRoute.of(context)!.settings.arguments as ScreenArguments;

    return Scaffold(
      appBar: AppBar(
        title: Text(args.title),
      ),
      body: Center(
        child: Text(args.message),
      ),
    );
  }
}

参考:https://docs.flutter.dev/cookbook/navigation/navigate-with-arguments

pgky5nke

pgky5nke3#

创建一个类似这样的类:

class ContactsDetailArguments {
  final ContactModel contactDetail;

  ContactsDetailArguments({
    @required this.contactDetail,
  });
}

并像这样更改您的ContactsDetail:

class ContactsDetail extends StatelessWidget {
      ContactsDetailArguments arguments;
      ContactsDetail({Key? key, required this.arguments}) : super(key: key);
...
}

然后在您的路由配置中执行以下操作:

var arg = settings.arguments as ContactsDetailArguments;
return MaterialPageRoute(builder: (context) => const LoginScreen(arguments: arg));

相关问题