首页 > 解决方案 > 如何将变量分配给颤振 Navigator.push(context, variable);

问题描述

我试图给它分配一个字符串 String variable = "'/branchHome'"; Navigator.push(上下文,变量);但它不适用于错误

Error: The argument type 'String' can't be assigned to the parameter type 'Route<Object>'.

我试着这样做 Navigator.push(context, variable as Route);

那也没用

如何在飞镖中将变量分配给路线名称

编辑: main.dart

void main() {
  runApp(MaterialApp(
    initialRoute: '/',
    routes: {
      '/': (context) => Loading(),
      '/home': (context) => Home(),
      '/studentHome': (context) => studentHome(),
      '/getAllstudents': (context) => getAllStudent(),
      '/examHome': (context) => getAllBranch(),
      '/branchHome': (context) => branchHome(),
      '/subjectHome': (context) => subjectHome(),
    },
  ));
}

列表

List<Info> slides = [
  Info(
    1,
    name: 'Branches',
    desc: "branches info",
    url: "'/branchHome'",
  ),
  Info(
    2,
    name: 'Exams',
    desc: "exam info",
    url: "'/examHome'",
  ),
];

我试过的

 onTap: () {
                  Navigator.push(context, slides[index].url);
                },

 onTap: () {
                  Navigator.push(context, slides[index].url as Route);
                },

标签: flutterdartroutes

解决方案


Navigator.pushNamed<dynamic>(context, slides[index].url);

这就像一个魅力


推荐阅读