flutter - 错误 : NoSuchMethodError, 在 null 上调用了 getter 'index'

标签 flutter dart

我正在使用 AnimatedContainer,以便它的高度可以(通过动画)适应其中存在的选项卡内容。我应该可以使用 DefaultTabController.of(context).index 访问选项卡的当前索引,并且我正在将此数据传输到一个函数,该函数将根据当前选项卡重建小部件。

当我运行代码时,它向我显示错误,但我不明白:这是否意味着 DefaultTabController 返回 null?

代码如下:

import 'package:flutter/material.dart';
import 'package:travel_agent_app/loginForm.dart';
import 'package:travel_agent_app/registerForm.dart';
import 'package:travel_agent_app/bubble_tab_indicator.dart';

class Login extends StatefulWidget {
  const Login({Key key}) : super(key: key);
  _LoginState createState() => _LoginState();

}


class _LoginState extends State<Login> {

  double _containerHeight = 300;



  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: 2,
        child: Scaffold(
            body: Container(
                child: Padding(
                  padding: const EdgeInsets.all(10.0),
                  child: Column(
                    children: <Widget>[
                      SizedBox(
                        height: 150,
                      ),
                      Container(
                        padding: EdgeInsets.only(left: 20.0, right:20.0),
                        child: Column(
                          children: <Widget>[
                            Container(
                              height: 52.0,
                              decoration: BoxDecoration(
                                color: Colors.grey[200],
                                borderRadius: BorderRadius.all(Radius.circular(100.0))
                              ),
                              child: TabBar(
                                indicatorSize: TabBarIndicatorSize.tab,
                                indicator: new BubbleTabIndicator(
                                    indicatorHeight: 45.0,
                                    indicatorColor: Colors.blueAccent,
                                    tabBarIndicatorSize: TabBarIndicatorSize.tab),
                                labelColor: Colors.white,
                                unselectedLabelColor: Colors.black,
                                onTap: setAnimatedContainerHeight(DefaultTabController.of(context).index),
                                tabs: <Widget>[
                                  Tab(text: 'Login'),
                                  Tab(text: 'Register'),
                                ],
                              ),
                            ),
                            SizedBox(
                              height: 20.0,
                            ),
                            AnimatedContainer(
                                duration: Duration(seconds: 1),
                                padding: EdgeInsets.all(40.0),
                                width: double.infinity,
                                height: _containerHeight,
                                decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(8.0),
                                    boxShadow: [
                                      BoxShadow(
                                          color: Colors.black12,
                                          offset: Offset(0.0, 15.0),
                                          blurRadius: 15.0),
                                    ]),
                                child: TabBarView(
                                  children: <Widget>[
                                    Container(
                                      width: 500.0,
                                      child: LoginForm(),
                                    ),
                                    RegisterForm(),
                                  ],
                                )
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                )
              )
            )
          );
  }

  setAnimatedContainerHeight(int index){
    if(index == 0){
      setState(() {
        _containerHeight = 300;  
      });
    }
    else{
      setState(() {
        _containerHeight = 450;  
      });
    }

  }
}

最佳答案

这里的问题是 DefaultTabController 上下文与定义 DefaultTabController.of(context).index 的上下文相同。

要解决错误 - 您需要在父上下文中定义 DefaultTabController

您的代码有效:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: DefaultTabController(    // Add here
        child: Login(),
        length: 2,
      ),
    );
  }
}

class Login extends StatefulWidget {
  const Login({Key key}) : super(key: key);
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
  double _containerHeight = 300;

  @override
  Widget build(BuildContext context) {
    return Scaffold(                      // Remove from here
        body: Container(
            child: Padding(
      padding: const EdgeInsets.all(10.0),
      child: Column(
        children: <Widget>[
          SizedBox(
            height: 150,
          ),
          Container(
            padding: EdgeInsets.only(left: 20.0, right: 20.0),
            child: Column(
              children: <Widget>[
                Container(
                  height: 52.0,
                  decoration: BoxDecoration(
                      color: Colors.grey[200],
                      borderRadius: BorderRadius.all(Radius.circular(100.0))),
                  child: TabBar(
                    indicatorSize: TabBarIndicatorSize.tab,
                    indicator: new BubbleTabIndicator(
                        indicatorHeight: 45.0,
                        indicatorColor: Colors.blueAccent,
                        tabBarIndicatorSize: TabBarIndicatorSize.tab),
                    labelColor: Colors.white,
                    unselectedLabelColor: Colors.black,
                    onTap: setAnimatedContainerHeight(
                        DefaultTabController.of(context).index),
                    tabs: <Widget>[
                      Tab(text: 'Login'),
                      Tab(text: 'Register'),
                    ],
                  ),
                ),
                SizedBox(
                  height: 20.0,
                ),
                AnimatedContainer(
                    duration: Duration(seconds: 1),
                    padding: EdgeInsets.all(40.0),
                    width: double.infinity,
                    height: _containerHeight,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(8.0),
                        boxShadow: [
                          BoxShadow(
                              color: Colors.black12,
                              offset: Offset(0.0, 15.0),
                              blurRadius: 15.0),
                        ]),
                    child: TabBarView(
                      children: <Widget>[
                        Container(
                          width: 500.0,
                          child: LoginForm(),
                        ),
                        RegisterForm(),
                      ],
                    )),
              ],
            ),
          )
        ],
      ),
    )));
  }

  setAnimatedContainerHeight(int index) {
    if (index == 0) {
      setState(() {
        _containerHeight = 300;
      });
    } else {
      setState(() {
        _containerHeight = 450;
      });
    }
  }
}

关于flutter - 错误 : NoSuchMethodError, 在 null 上调用了 getter 'index',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56649654/

相关文章:

dart - 在 Dart 语言列表中找到索引值

android - flutter/FireStore : how to display an image from Firestore in Flutter?

flutter - 如何在 flutter 中制作自定义小部件/组件?

android - Scaffold Messenger 问题 : The method 'showSnackBar' was called on null

dart - 我怎样才能让 BottomNavigationBar 粘在键盘 flutter 的顶部

mocking - 是否可以模拟只公开工厂构造函数的类?

javascript - 对 Javascript、PHP 和 Dart 进行基准测试

ios - Flutter IOS应用程序不会以image_picker开头:^ 0.6.3 + 4

flutter - 按下按钮获取数据

flutter - 如何在flutter中使用Cardview中的Listview?