0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Flutter】Routeがうまく動作しなかったのを解決!

Posted at

ルーティングで 下記のように設定したにもかかわらず、


routes: <String, WidgetBuilder>{
  '/': (_) => new Home(),
  '/register': (_) => new Register(),
  '/list': (_) => new List(),
},

このコードで、なぜかページ遷移できませんでした。


Navigator.of(context).pushNamed("/record")

これの問題がわかって、解決したので、記事に残しておきます!

問題

問題はMaterialAppが2個あったことでした。

Mainのクラスはこうなっています


void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override

  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      routes: <String, WidgetBuilder>{
        '/': (_) => new Home(),
        '/register': (_) => new Register(),
        '/list': (_) => new List(),
      },
    );
  }
}

で、肝心のListクラスはこうなっていました。


class List extends StatelessWidget {
 @override

  Widget build(BuildContext context) {
   return MaterialApp(
     debugShowCheckedModeBanner: false,
     home: Scaffold(
       appBar: Header(),
       drawer: CustomDrawer(),
       body: Container(
       .
       .
       .
       ),
       bottomNavigationBar: Footer()
     ),
   );
 }
}

見ての通りMaterialAppが2つあります。これが原因でした。

解決策

Listの方のMaterialAppを削除します

class List extends StatelessWidget {
  @override

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Header(),
      drawer: CustomDrawer(),
      body: Container(
      .
      .
      .
      ),
      bottomNavigationBar: Footer()
    );
  }
}

これでルーティングが正常に動くようになります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?