is it right way to handle variables inside @build method in flutter?

313

According to my knowledge, it is not the proper way of coding to declare or manage variables inside the build method.

Share:
313
chiragmevada
Author by

chiragmevada

Updated on December 24, 2022

Comments

  • chiragmevada
    chiragmevada over 1 year

    i think sometimes it is required to handle variables inside @override build method.

    @override
      Widget build(BuildContext context) {
        Animation animationTitle1 = Tween<double>(begin: 0.0, end: 1.0).animate(
            CurvedAnimation(
                parent: widget.animationController,
                curve: Interval(0, 1.0, curve: Curves.fastOutSlowIn)));
        Animation animationTitle2 = Tween<double>(begin: 0.0, end: 1.0).animate(
            CurvedAnimation(
                parent: widget.animationController,
                curve: Interval(0.2, 1.0, curve: Curves.fastOutSlowIn)));
    .
    .
    .
    

    i don't know it is logically efficient or not ? but somehow i need this kind of logic to declare and manage multiple variables inside @build method. some requirements like

    • animations objects
    • call stream builder widget manually in some case to refresh the data

    example like

    @override
      Widget build(BuildContext context) {
        var streamBuilder = StreamBuilder(
              stream: myStreamObj,
              builder: (BuildContext context, AsyncSnapshot<dynamic> snapshotVacchan) {
               // my code
              },
            );
        
        return Column(
                children: [
                  streamBuilder,
                  FlatButton(
                      child: Text('Reload'),
                      onPressed: () {
                        setState(() {
                          myStreamObj = newScreen;
                        });
                      }),
                ],
              );
     }