安全——延迟初始化错误 // FLUTTER

safety -- late initialization error // FLUTTER

I have an error in the code because of safety in Flutter and I have tried to solve it with the declaration of variables using LATE.<br /

But it appears antother one when I try to build it:

Error

This is my code:

class AnimacionesPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CuadradoAnimado(),
      ),
    );
  }
}

class CuadradoAnimado extends StatefulWidget {
  @override
  _CuadradoAnimadoState createState() => _CuadradoAnimadoState();
}

class _CuadradoAnimadoState extends State<CuadradoAnimado>
    with SingleTickerProviderStateMixin {
  late AnimationController controller;
  late Animation<double> rotacion;

@override
  void initState() {
    controller = new AnimationController(
        vsync: this, duration: Duration(milliseconds: 4000));

    rotacion = Tween(begin: 0.0, end: 2 * Math.pi)
        .animate(CurvedAnimation(parent: controller, curve: Curves.easeOut));

 @override
  Widget build(BuildContext context) {
    // Play / Reproducción
    controller.forward();

    return AnimatedBuilder(
      animation: controller,
      child: _Rectangulo(),

试试这个

     class _CuadradoAnimadoState extends State<CuadradoAnimado>
                with SingleTickerProviderStateMixin {
              AnimationController? controller;
              Animation<double>? rotacion;
            
            @override
              void initState() {
                controller = new AnimationController(
                    vsync: this, duration: Duration(milliseconds: 4000));
            
                rotacion = Tween(begin: 0.0, end: 2 * Math.pi)
                    .animate(CurvedAnimation(parent: controller, curve: Curves.easeOut));
             controller.forward();
setState((){});
    }
             @override
              Widget build(BuildContext context) {
                // Play / Reproducción
               
            
                return controller==null && rotacion==null ? CircularProgressIndicator.adaptive():  AnimatedBuilder(
                  animation: controller!,
                  child: _Rectangulo());

嗨 Ignacio,欢迎来到 Whosebug 社区

它应该运行良好,但尝试纠正一些问题。

首先,这是一段代码,可以解释您想要执行的操作:

import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: const Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatefulWidget {
  const MyWidget({Key? key}) : super(key: key);

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

class _MyWidgetState extends State<MyWidget>
    with SingleTickerProviderStateMixin {
  late AnimationController controller;
  late Animation<double> rotacion;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 4000));

    rotacion = Tween<double>(begin: 0.0, end: 250)
        .animate(CurvedAnimation(parent: controller, curve: Curves.easeOut));
    controller.forward();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: AnimatedBuilder(
          animation: rotacion,
          builder: (context, _) {
            return Container(
              margin: const EdgeInsets.symmetric(vertical: 10),
              height: rotacion.value,
              width: rotacion.value,
              child: const FlutterLogo(),
            );
          }),
    );
  }
}

第一件事(你可能会忘记添加它,但这只是一个提醒),确保关闭 init 状态的花括号和任何其他语法问题。

第二个是你应该在init状态下转发控制器,而不是在构建中,因为你想第一次转发它,而不是每次构建组件时都转发。

第三点是我要在其中制作动画的小部件,我用另一个名为 AnimatedBuilder 的小部件将其包装:它是一个 general-purpose 用于构建动画的小部件(还有其他方法可以为当然)。

最后一点是不要忘记释放你的控制器,因为你需要释放这个对象使用的资源,因为当小部件被释放时,这个对象就不再可用了。

如果您完成所有这些操作(并确保阅读代码片段),它将运行良好。