Thanks for your attention. I'm a beginner of flutter. I don't know why the initState function isn't called by default. Because of the print(list[0]) statement is not be run.
import 'package:flutter/material.dart';
import 'main_page/main_page.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MyHomePage();
}
class _MyHomePage extends State<MyHomePage> {
int _currentIndex = 0;
List<Widget> list = List();
@override
void initState() {
list.add(MainPage());
print(list[0]);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: MainPage(),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home')
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text('Me')
),
],
currentIndex: _currentIndex,
onTap: (int index) {
setState(() {
_currentIndex = index;
});
},
type: BottomNavigationBarType.fixed,
),
);
}
}