Flutter:调用 Stateful Widget 时没有 SuchMethodError

Flutter : No SuchMethodError when I called Stateful Widget

我是 Flutter 新手。我想制作一个显示 BottomNavigationBar 选择的页面的应用程序。 但是当我尝试 运行 应用程序时,它会抛出异常。以下是错误日志。

════════ 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)

我的代码在这里

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")),
    );
  }
}

在 MyApp 中,MainPages() 被称为 MaterialApp 的主页。那时,它抛出一个异常。
在 MainPagesState class 中,build() 函数首先初始化 App Configuration。
然后构建Scaffold Widget,包括DailyAppBar(我自定义的Appbar)、pages[_currentIndex]、bottomNavigationBar。每日 AppBar 和 pages[0] 使用 AppConfig 数据。

使用init_appConfig或bottomNavigationBar时是否有错误?
Appconfig、SubjectListTile、SubjectList 和 State、DailyAppBar 当我将 SubjectList() 直接放在 Scaffold 的主体中时效果很好。

您错过了 return 声明。

 @override
  _MainPagesState createState() {
    return _MainPagesState();
  }

或者只使用箭头函数

@override
  _MainPagesState createState() => _MainPagesState();