pushReplacementNamed在Drawer上不能正常工作。

问题描述 投票:0回答:1

我是一个初学者在翩翩和我做一个APP,但我有这个问题。

在main中,我写了这段代码。

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(),
            ),
          ),
        ],
      ),
    );
  }
}

但是当我运行App并点击 "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(''); "时,它顺利进入主页。

请帮帮我!

我在这里希望得到任何澄清。

谅谅

android visual-studio flutter routes drawer
1个回答
0
投票

从我们的讨论来看,你在SelectionPages中也使用了MaterialApp小组件,在那里你没有定义路由,但是导航器在他们身上寻找路由。

只要移除SelectionPages中的MaterialApp widget就可以解决你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.