Home > Software design >  Flutter How Can I reach another class func?
Flutter How Can I reach another class func?

Time:08-12

class _AnimatedLiquidCustomProgressIndicatorState
    extends State<_AnimatedLiquidCustomProgressIndicator>
    with SingleTickerProviderStateMixin {
   late AnimationController  _animationController;


  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,

      duration: Duration(seconds: 10),
    );

  }
   void checkWork() {
     _animationController.repeat();
     _animationController.addListener(() => setState(() {}));
   }

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

  @override
  Widget build(BuildContext context) {
    final percentage = _animationController.value * 100;
   
    if(per <= _animationController.value * 100){
      _animationController.stop();
    }

    return Row(
      children: [
        LiquidCustomProgressIndicator(
          value: _animationController.value ,
          direction: Axis.vertical,
          backgroundColor: Colors.white,
          valueColor: AlwaysStoppedAnimation(Colors.red),
          shapePath: _buildHeartPath(),
          center: Text(
            "${percentage.toStringAsFixed(0)}%",
            style: TextStyle(
              color: Colors.black,
              fontSize: 20.0,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
   
      ],


    );

I want to call "checkWork" function in another class button but when I tried this I get error "LateInitializationError: Field '_animationController@29217585' has not been initialized." Actually I don't know how to reach that function. What should I do ?

CodePudding user response:

You have two issues, first of all your _AnimatedLiquidCustomProgressIndicatorState class is private and that is why you can't call it from another class. Unless your other class is located in the same file as _AnimatedLiquidCustomProgressIndicatorState.

You have two options:

Keep private class:

If you want to keep it private, you can create the other class in the same file where your private class exists. It would look something like this:

    class AnotherClass {
        // ... Whatever you want to do in here
        final animatedProgressIndicator =
            _AnimatedLiquidCustomProgressIndicatorState() // You create the object.

        void someFunction() {
            // How you can now call the method.
            animatedProgressIndicator.checkWork();
        }
    }

    class _AnimatedLiquidCustomProgressIndicatorState
    extends State<_AnimatedLiquidCustomProgressIndicator>
        with SingleTickerProviderStateMixin {
        // ...
        void checkWork() {
            // Whatever you want to execute...
        }
        // ...
    }

If not, you can also:

Change your class to public:

Just remove _ from your class name, making it now AnimatedLiquidCustomProgressIndicatorState. Here you can do the same as before, but AnotherClass can now be in a different file.

Finally, the "LateInitializationError: Field '_animationController@29217585' has not been initialized." error it's because of this:

Instead of initializing your AnimationController with late, like:

late AnimationController  _animationController;

do this:

AnimationController?  _animationController;

That should do it.

  • Related