即使我在 initState 中初始化变量,Dart 也会抛出 LateInitializationError

Dart throws LateInitializationError even when I initialize variables in initState

我使用 late 关键字声明了两个变量,这样我就可以在 initState 函数中初始化它们。

class _CustomNavBarState extends State<CustomNavBar>
    with TickerProviderStateMixin {

  late AnimationController _animationController;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    AnimationController _animationController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 500));
    Animation<double> _animation =
        Tween<double>(begin: 1, end: 0.8).animate(_animationController);
  }
//...

如果 initState 在任何其他函数之前被调用,为什么会出现以下错误?

LateIinitializationError: Field '_animation@17200479' has not been initialized.

这里还有一些代码:

class CustomNavBar extends StatefulWidget {
  const CustomNavBar(
      {required this.icons,
      required this.names,
      required this.onPressed,
      required this.activeIndex});
  final List<IconData> icons;
  final List<String> names;
  final Function(int) onPressed;
  final int activeIndex;

  @override
  _CustomNavBarState createState() => _CustomNavBarState();
}

class _CustomNavBarState extends State<CustomNavBar>
    with TickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _animation;

  @override
  void didUpdateWidget(CustomNavBar oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.activeIndex != widget.activeIndex) {
      _animate();
    }
  }

  void _animate() {
    _animationController.forward();
  }

  @override
  void initState() {
    super.initState();
    AnimationController _animationController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 500));
    Animation<double> _animation =
        Tween<double>(begin: 1, end: 0.8).animate(_animationController);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 90,
      decoration: BoxDecoration(
          gradient: LinearGradient(
        colors: [Theme.of(context).backgroundColor, Colors.black],
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
      )),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          for (var i = 0; i < widget.icons.length; i++)
            TextButton(
                style:
                    TextButton.styleFrom(splashFactory: NoSplash.splashFactory),
                onPressed: () => widget.onPressed(i),
                child: ScaleTransition(
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(
                            widget.icons[i],
                            size: 30,
                            color: i == widget.activeIndex
                                ? Theme.of(context).accentColor
                                : Colors.white70,
                          ),
                          Text(
                            widget.names[i],
                            style: TextStyle(
                              color: i == widget.activeIndex
                                  ? Theme.of(context).accentColor
                                  : Colors.white70,
                            ),
                          )
                        ]),
                    scale: _animation))
        ],
      ),
    );
  }
}


这是在声明一个 local 变量:

AnimationController _animationController =
    AnimationController(vsync: this, duration: Duration(milliseconds: 500));

你想要的是分配给你现有的 class 成员变量:

_animationController =
    AnimationController(vsync: this, duration: Duration(milliseconds: 500));