BottomNaviationBarを必要最低限書いて実行したところ。実行時エラーとなった。コンパイルエラーにはなっていなかった。
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Home(),
);
}
}
class Home extends StatelessWidget {
const Home({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home'),),
bottomNavigationBar: BottomNavigationBar(
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home),),
BottomNavigationBarItem(icon: Icon(Icons.article),),
BottomNavigationBarItem(icon: Icon(Icons.accessibility),),
],
),
);
}
}
原因は、BottomNavigationBarItemの属性であるlabelが未設定(null)となったことで実行時エラー。コンパイル時にチェックはできないものなのだろうか。Flutterの仕様を理解すれば当たり前のことなのか、まだこれから学んで行きたいと思う。以下のように、適当なlabelを追記する。
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home',),
BottomNavigationBarItem(icon: Icon(Icons.article), label: 'Article',),
BottomNavigationBarItem(icon: Icon(Icons.accessibility), label: 'Settings',),
今度は期待の通りアイコンが下に表示された。もちろん、onTapを実装していないので、アイコンを押しても画面の切り替わりなどは一切ない。