Reputation: 25
I'm new to flutter. I want to make an app that shows a page selected by BottomNavigationBar. But when I'm trying to run the app, it throws an Exception. The following is Error Log.
════════ Exception caught by widgets library
The following NoSuchMethodError was thrown building Builder:
The method '_debugTypesAreRight' was called on null.
Receiver: null
Tried calling: _debugTypesAreRight(Instance of 'MainPages')
The relevant error-causing widget was:
MaterialApp file:///C:/Users/jango/AndroidStudioProjects/study_and_statistic/lib/main.dart:49:14
When the exception was thrown, this was the stack:
0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
1 new StatefulElement.<anonymous closure> (package:flutter/src/widgets/framework.dart:4309:19)
2 new StatefulElement (package:flutter/src/widgets/framework.dart:4320:6)
3 StatefulWidget.createElement (package:flutter/src/widgets/framework.dart:809:38)
4 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3189:40)
and my code is here
main.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class AppConfig {
static double width;
static double height;
static double blockSize;
static double blockSizeVertical;
static double statusBarHeight;
static double getAppbarHeight(){
double ratioHeight = blockSizeVertical*9;
return (ratioHeight>60)? 60 : ratioHeight;
}
static double getGap(){
double ratioGap = width/20;
return (ratioGap>30)? 30 : ratioGap;
}
static double getFontsize_content(){
double ratioSize = (blockSize>blockSizeVertical)?blockSizeVertical*6:blockSize*6;
return (ratioSize > 18)? 18: ratioSize;
}
static double getFontsize_appBar(){
double ratioSize = (blockSize>blockSizeVertical)?blockSizeVertical*7:blockSize*7;
return (ratioSize > 20)? 20: ratioSize;
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){
FocusScope.of(context).unfocus();
},
child: MaterialApp(
title: 'STUDY',
theme: ThemeData(
fontFamily: 'NanumBarunGothic',
primaryColor: Color(0XFF5dc19b),
),
home: MainPages() //Here is the problem, maybe..
)
);
}
}
PreferredSize DailyAppBar(){
//My customAppBar
//AppConfig used here
}
class SubjectListTile extends StatelessWidget{
//My custom ListTile
//AppConfig used here
}
class SubjectList extends StatefulWidget{
@override
State createState() => SubjectListState();
}
class SubjectListState extends State<SubjectList>{
//My custom Listview
}
class MainPages extends StatefulWidget{
const MainPages({ Key key }) : super(key: key);
@override
_MainPagesState createState() {
_MainPagesState();
}
}
class _MainPagesState extends State<MainPages>{
int _currentIndex = 0;
final List<Widget> pages = [
SubjectList(),
StudyPage(),
StaticPage(),
];
void init_AppConfig(BuildContext context){
AppConfig.width = MediaQuery.of(context).size.width;
AppConfig.height = MediaQuery.of(context).size.height;
AppConfig.blockSize = AppConfig.width / 100;
AppConfig.blockSizeVertical = AppConfig.height / 100;
AppConfig.statusBarHeight = MediaQuery.of(context).padding.top;
double width = AppConfig.width;
double height = AppConfig.height;
print('width: $width');
print('height: $height');
}
void _onItemTapped(int index){
setState((){
_currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
init_AppConfig(context);
return Scaffold(
appBar: DailyAppBar(),
body : pages[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: _onItemTapped,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.check_box),
title: Text('오늘의 공부'),
),
BottomNavigationBarItem(
icon: Icon(Icons.chrome_reader_mode),
title: Text('집중모드'),
),
BottomNavigationBarItem(
icon: Icon(Icons.show_chart),
title: Text('기록'),
),
],
),
);
}
}
class StaticPage extends StatelessWidget{ //Not impleted yet
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child:Text("StaticPage")),
);
}
}
class StudyPage extends StatelessWidget{ //Not impleted yet
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child:Text("StudyPage")),
);
}
}
In MyApp, MainPages() is called as home of MaterialApp. At that time, it throws an Exception.
In MainPagesState class, build() function initializes App Configuration first.
And then it builds Scaffold Widget, which includes DailyAppBar(my custom Appbar), pages[_currentIndex], bottomNavigationBar. Daily AppBar and pages[0] use AppConfig Data.
Is there a mistake when using init_appConfig or bottomNavigationBar?
Appconfig, SubjectListTile, SubjectList and State, DailyAppBar worked well when I put SubjectList() in body of Scaffold directly.
Upvotes: 1
Views: 2676
Reputation: 1122
You have missed the return statement.
@override
_MainPagesState createState() {
return _MainPagesState();
}
or just use arrow function
@override
_MainPagesState createState() => _MainPagesState();
Upvotes: 3