pushReplacementNamed 在抽屉上无法正常工作

pushReplacementNamed doesn't work correctly on Drawer

我是 flutter 的初学者,我正在制作一个 APP,但我遇到了这个问题:

主要是我写了这段代码:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(
          value: ImgProvider(),
        ), // create the provider of images
        ChangeNotifierProvider.value(
          value: Saved(),
        ), // create the provider of Saved
      ],
      child: MaterialApp(
        title: 'HTrip',
        theme: ThemeData(
          primarySwatch: Colors.green,
          accentColor: Colors.yellowAccent,
        ),
        home: SelectionPages(),
        routes: {
          '/saved'  :   (context) => SavedScreen(),
        }, // define the main page to be displayed
      ),
    );
  }
}

在 SelectionPages() 中,我这样写:

drawer: Account(),

并且在 Account() 中,我这样写:

class Account extends StatelessWidget {
  Account();
  @override
  Widget build(BuildContext context) {
    final savedItem = Provider.of<Saved>(context);
    return Drawer(
          child: Column(
            children: <Widget>[
              AppBar(
          title: Text("Account"),
          automaticallyImplyLeading: false,
          centerTitle: true,
        ),
        Divider(),
        ListTile(
                leading: Icon(Icons.sd_card),
                title: Text("Saved"),
                onTap: () {
                  debugPrint("Hello button is clicked");
                  Navigator.of(context).pushReplacementNamed('/saved');
                  },
                // dense: true,
                trailing: Chip(
                  label: Text('${savedItem.savedCount}'),
                  labelStyle: TextStyle(color: Theme.of(context).primaryColor),
                ),
                //  enabled: true,
              )
            ]
          )
    );
  }
}

在 SavedScreen() 中,我这样写:

class SavedScreen extends StatefulWidget {
  //static const routeName = '/Saved';

  @override
  _SavedScreenState createState() => _SavedScreenState();
}

class _SavedScreenState extends State<SavedScreen> {
  @override
  Widget build(BuildContext context) {
    final savedItem = Provider.of<Saved>(context);
    return Scaffold(
      appBar: AppBar(title: Text("Saved")),
      body: Column(
        children: <Widget>[
          Card(
            margin: const EdgeInsets.all(15),
            child: Padding(
              padding: const EdgeInsets.all(8),
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(
                      'Saved item',
                      style: TextStyle(fontSize: 20),
                    ),
                    SizedBox(width: 10),
                    Spacer(), // to take all the empty space available in this place
                    Chip(
                      label: Text('${savedItem.savedCount}'),
                    ),
                    FlatButton(onPressed: () {}, child: Text("See all")),
                  ]),
            ),
          ),
          Expanded(
            child: Card(
              child: ImgSavedGridView(),
            ),
          ),
        ],
      ),
    );
  }
}

但是当我 运行 应用程序并单击 "Saved" (我 运行 这个 "Navigator.of(context).pushReplacementNamed('/saved');" 的地方)我得到了这个错误 :

════════════════════════════════════════════════════════════════════════════════
I/flutter (27612): Hello button is clicked

════════ Exception caught by gesture ═══════════════════════════════════════════
Could not find a generator for route RouteSettings("/saved", null) in the _WidgetsAppState.
════════════════════════════════════════════════════════════════════════════════

PS :当我将 "Navigator.of(context).pushReplacementNamed('/saved');" 更改为 "Navigator.of(context).pushReplacementNamed('/');" 时。它可以毫无问题地进入主页

请帮助我!

我来这里是为了澄清。

谢谢

根据我们的讨论,您也在 SelectionPages 中使用了 MaterialApp 小部件,您也没有在其中定义路由,但导航器会在其上查找路由。

只需删除 SelectionPages 中的 MaterialApp 小部件即可解决您的问题。