建立TabBar时,将getter'length'调用为null

时间:2019-04-01 13:13:46

标签: dart flutter

第一次从Flutter应用程序加载TabBar时出现以下错误:

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following NoSuchMethodError was thrown building TabBar(dependencies: [_InheritedTheme,
flutter: _LocalizationsScope-[GlobalKey#c29e1], _TabControllerScope], state: _TabBarState#dd10a):
flutter: The getter 'length' was called on null.
flutter: Receiver: null
flutter: Tried calling: length
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
flutter: #1      _IndicatorPainter._tabOffsetsEqual 
package:flutter/…/material/tabs.dart:414
flutter: #2      _IndicatorPainter.shouldRepaint 
package:flutter/…/material/tabs.dart:427
flutter: #3      RenderCustomPaint._didUpdatePainter 
package:flutter/…/rendering/custom_paint.dart:433
flutter: #4      RenderCustomPaint.painter= 
package:flutter/…/rendering/custom_paint.dart:398
flutter: #5      CustomPaint.updateRenderObject 
package:flutter/…/widgets/basic.dart:481
flutter: #6      RenderObjectElement.update 
package:flutter/…/widgets/framework.dart:4510
flutter: #7      SingleChildRenderObjectElement.update 
package:flutter/…/widgets/framework.dart:4881
(...)

它在用户看来仅持续几秒钟,然后一切恢复正常,并且标签控制器正常工作。我尝试创建自己的TabController,但仍然遇到相同的错误

我的代码:

class HomeScreen extends StatelessWidget {
  HomeScreen();

  @override
  Widget build(BuildContext context) {

    return SafeArea(
      child: DefaultTabController(
        length: 5,
        initialIndex: 0,
        child: Scaffold(
          backgroundColor: Color(0xFF25272b),
          appBar: TabBar(
            tabs: [
              Tab(icon: Icon(Icons.home)),
              Tab(icon: Icon(Icons.settings)),
              Tab(icon: Icon(Icons.media)),
              Tab(icon: Icon(Icons.account_box)),
              Tab(icon: Icon(Icons.notifications)),
            ],
          ),
          body: TabBarView(
            physics: NeverScrollableScrollPhysics(),
            children: [
              Content1(),
              Content2(),
              Content3(),
              Content4(),
              Content5(),
            ],
          ),
        ),
      ),
    );
  }
}

我试图评论TabBar和TabBarView的内容。因此,我认为这不是我创建的一些自定义小部件产生的错误。

关于如何解决此问题的任何想法?

编辑:我正在使用Redux。我不知道这是否会影响问题。这是我的main.dart:

class MainAppState extends State<MainApp> {
  Persistor<AppState> persistor;
  Store<AppState> store;

  void initState() {
    super.initState();

    persistor = Persistor<AppState>(
      storage: FlutterStorage(),
      serializer: JsonSerializer<AppState>(AppState.fromJson),
    );

    store = Store<AppState>(
      appReducer,
      initialState: AppState(),
      middleware  : createStoreMiddleware(
        widget.navigatorKey,
      )
      ..add(persistor.createMiddleware()),
    );

    persistor.load()
      .whenComplete(() => store.dispatch(InitAuth()) );
  }

  @override
  Widget build(BuildContext context) {

    return StoreProvider(
      store: store,
      child: MaterialApp(
        locale: Locale('en'),
        debugShowCheckedModeBanner: false,
        title: 'app',  
        theme: AppTheme.theme,
        navigatorKey: widget.navigatorKey,
        initialRoute: store.state.user == null
          ? LoginScreen.route
          : HomeScreen.route,
        routes: <String, WidgetBuilder> {
          HomeScreen.route  : (context) {
            return StoreBuilder<AppState>(
              builder: (context, store) {
                return HomeScreen();
              },
            );
          },

2 个答案:

答案 0 :(得分:0)

您已经检查了目录,没有TabBarView中的内容部分,您的代码可以完美运行

    body: TabBarView(
        physics: NeverScrollableScrollPhysics(),
        children: [
          Content1(), <--------------
          Content2(), <--------------
          Content3(), <--------------
          Content4(), <--------------
          Content5(), <--------------
        ],
      ),
    ),

替换为

       body: TabBarView(
        physics: NeverScrollableScrollPhysics(),
        children: [
         Text("1"),
         Text("2"),
         Text("3"),
         Text("4"),
         Text("5"),
        ],
      ),
    ),

没有错误。

答案 1 :(得分:0)

嗯...我发现了问题。这与Flutter如何在没有自定义键的情况下重用窗口小部件有关。

我在HomeScreen和LoginScreen之间进行了较差的路由更改:

我具有此功能来更新Page:

navigatorKey.currentState.pushNamedAndRemoveUntil(action.name, ModalRoute.withName('/'));

我更改为:

navigatorKey.currentState.pushNamedAndRemoveUntil(action.name, (_) => false);

因此,可能是该应用从HomeScreen保存了一些垃圾数据,直到它重新生成具有理想状态的新小部件为止。