flutter exception: Cannot lerp between "0" and "1"

Viewed 1133

how are you? i'm learning flutter and i'm bloqued by flutter excetion and i don't know how resolve it.I want to make a progressBar for a quizz gma. i would like to make a 60 secondes progress bar but When i'm execute my code i get this error:

FlutterError (Cannot lerp between "0" and "1".
The type int returned a double after multiplication with a double value. See "Types with 
special considerations" at https://api.flutter.dev/flutter/animation/Tween-class.html for more 
information.
To lerp int values, consider IntTween or StepTween instead.)

anyone can help me?? that my ProgressBar code

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:quizz_app/constants.dart';
import 'package:quizz_app/controllers/question_controllers.dart';

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

 @override
 Widget build(BuildContext context) {
 return Container(
  width: double.infinity,
  height: 35,
  decoration: BoxDecoration(
      border: Border.all(color: Colors.white.withOpacity(0.2), width: 3),
      borderRadius: BorderRadius.circular(50)),
  child: GetBuilder<QuestionController>(
    init: QuestionController(),
    builder: (controller) {
      print(controller.animation.value);
      return Stack(
        children: [
          LayoutBuilder(
              //layout provide us the available space for the container
              builder: (context, constraints) => Container(
                    width: constraints.maxWidth * 0.5,
                    decoration: BoxDecoration(
                        gradient: kPrimaryGradient,
                        borderRadius: BorderRadius.circular(50)),
                  )),
          Positioned.fill(
              child: Padding(
            padding:
                const EdgeInsets.symmetric(horizontal: kDefaultPadding),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              // ignore: prefer_const_literals_to_create_immutables
              children: [
                const Text("Time left:", style: TextStyle(fontSize: 20)),
                const Text(
                  "10",
                  style: TextStyle(fontSize: 20),
                ),
              ],
            ),
          ))
        ],
      );
    },
  ),
);
 }
}

and this is my Question controller code for control my progress bar

import 'package:flutter/animation.dart';
import 'package:get/get.dart';

  //we use get pckage for our state management
class QuestionController extends GetxController
  with SingleGetTickerProviderMixin {
 late AnimationController _animationController;
 late Animation _animation;
 Animation get animation => this._animation;

 @override
 void onInit() {
 _animationController =
    AnimationController(duration: const Duration(seconds: 60), vsync: this);
_animation = Tween(begin: 0, end: 1).animate(_animationController)
  ..addListener(() {
    //update like setstate
    update();
  });
_animationController.forward();
super.onInit();
 }
}
3 Answers

change Tween(begin: 0, end: 1) to Tween(begin: 0.0, end: 1.0)

In case of int you should use IntTween, not regular Tween.

Tween interpolates between two values. It is a generic type; if you don't explicitly specify the type parameter, it will be inferred from the construction arguments. Since you used Tween(begin: 0, end: 1), so the concrete type is inferred to be Tween<int> from the 0 and 1 integer literals. You can't interpolate between 0 and 1 using ints, so you either must use double literals so that the type is inferred to be Tween<double>:

Tween(begin: 0.0, end: 0.0)

or explicitly request a Tween<double>:

Tween<double>(begin: 0, end: 0)
Related