Search code examples
flutterdartwidgetnull-safety

How to return in animatedbuilder with null-safety?


can anyone help me with this i am learning how to make carousel from techie blossom channel playlist Error is below in the screen shot I am struggling with null safety thing can anyone resolve this problem. İ tried with null-safety but couldn't succeed thank you 🙏🏼enter image description here


Solution

  • Wrap your child inside builder with Container().

    //...
    builder: (BuildContext context, Widget? child) {
        return Container(child: child);
      },
    //...
    

    Here is the sample example of how to use AnimatedBuilder()

    class MyWidget extends StatefulWidget {
      @override
      _MyWidgetState createState() => _MyWidgetState();
    }
    
    class _MyWidgetState extends State<MyWidget> with 
    TickerProviderStateMixin {
      late final AnimationController _controller = AnimationController(
      duration: const Duration(seconds: 10),
      vsync: this,
    )..repeat();
    @override
    Widget build(BuildContext context) {
      return AnimatedBuilder(
        // This widget wont rebuild.
        child: Container(
          child: Text("I can not rebuild weeeee.....!!!"), color: Colors.green),
      animation: _controller,
      builder: (BuildContext context, Widget? child) {
        return Transform.rotate(
          angle: _controller.value * 2.0 * math.pi,
          // here you will pass your widget
          child: child,
         );
        },
      );
     }
    }