1
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でif文を使うときの注意事項

Last updated at Posted at 2020-06-12

多言語(Jave、PHPなど)の場合

僕の経験上(浅いので突っ込まないで)if文で条件分岐する際に、
条件がtrueの時しか書いてこなかったんですが、Flutterで画面遷移を作成しているときに
引っかかったのでメモとして残しておきます。

PHPのとき

    if (index == 1) self::NextPage();

みたいな感じで、indexが1だったときNextPageメソッドを呼び出すことをよくしていました。

Laravelでリダイレクトするならこんな感じ

    if (index == 1) return redirect()->route('next_page');

この時って別にfalseだった時の条件分岐書いてこなかったんですよね、
((まぁ書いた方がいいんですけど))

Flutter

はい、本題です。

if (index == 1) Navigator.of(context).pushNamed('/search');

でBottomNavigationBarがonTapされたときに、画面遷移したかったのですが、
trueの時だけ書いていたら、
A build function returned nullみたいなエラーが。。
Flutter初学者の私からしたらOMGでした。

falseの条件分岐を記載したらエラー解決しました!

onTap: (int index) {
            print(index);
            if (index == 1) Navigator.of(context).pushNamed('/search');
            else Navigator.of(context).pushNamed('/');
        },
1
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
1
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?