Mobile 颤振选项卡正在引发空指针异常->;Material.of(context).color的计算结果为空

Mobile 颤振选项卡正在引发空指针异常->;Material.of(context).color的计算结果为空,mobile,dart,flutter,Mobile,Dart,Flutter,我遵循的是颤振图库中提供的expansiona_panels_demo.dart示例。我的目标是在扩展面板主体中引入TabBar和TabBarView。到目前为止,这是因为tabs.dart中引发了空指针异常。这是在_TabBarState中,它正在与小部件.indicatorColor进行比较,如果您要查看演示,我将在第251行尝试调用new BookTabs() if (color.value == Material.of(context).color.value) 此处Material.

我遵循的是颤振图库中提供的expansiona_panels_demo.dart示例。我的目标是在扩展面板主体中引入TabBar和TabBarView。到目前为止,这是因为tabs.dart中引发了空指针异常。这是在_TabBarState中,它正在与
小部件.indicatorColor
进行比较,如果您要查看演示,我将在第251行尝试调用new BookTabs()

if (color.value == Material.of(context).color.value)
此处Material.of(context).color为空

为了整合代码,我将标签放在一个有状态的小部件中。当我将小部件放置在gallery示例中定义的可折叠体小部件中时,就会抛出调用null*的*getter'value'。目前我不知道如何解决这个问题。如有任何建议,将不胜感激。谢谢

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class BookTabs extends StatefulWidget {
   @override
  _BookTabs createState() => new _BookTabs();
}

class _BookTabs extends State<BookTabs> with 
SingleTickerProviderStateMixin {

TabController _tabController;

@override
void initState() {
 super.initState();
 _tabController = new TabController(vsync: this, length: 2);
}

@override
Widget build(BuildContext context) {
 return new Column(
   children: <Widget>[
    new TabBar(
        controller: _tabController,
        indicatorColor: Colors.blue,
        labelStyle: new TextStyle(color: Colors.blue),
        labelColor: Colors.blue,
        unselectedLabelColor: Colors.indigo,             
        tabs: [
          new Tab(text: 'Books'),
          new Tab(text: 'Authors')
        ]
    ),
    new Expanded(
        child: new TabBarView(
            controller: _tabController,

            children: [
              new SafeArea(
                  top: false,
                  bottom: false,
                  child:
                  new Text("Books", style: Theme
                      .of(context)
                      .textTheme
                      .subhead)),

              new SafeArea(
                  top: false,
                  bottom: false,
                  child: new Text("Authors", style: Theme
                      .of(context)
                      .textTheme
                      .subhead))
            ])
    )
   ],
  );
 }
}
我认为您不使用作为
图书标签的父项


如果你不想要
材质app
,你至少需要将它包装在中。我在使用
Scaffold.of(context)
时遇到了类似的问题,并通过将我的所有小部件包装在
Builder
包装器中来解决


检查文档中的操作方法,当你需要一个有效的上下文时,这是最好的方法

将小部件包装在材质类中修复了该问题

new Material(color: Theme
                        .of(context)
                        .dialogBackgroundColor, child: new BookTabs()
                    )

您是否有
MaterialApp
作为
booktab
的母公司?谢谢!用材料包裹元素修复了问题!再次感谢,这对我来说很难,谢谢!代码在一个构建器中,我只需要将其包装在一个材质常量中。
new Material(color: Theme
                        .of(context)
                        .dialogBackgroundColor, child: new BookTabs()
                    )