Should I use getters for building widget sub-tree?

162

I'm going to compare this method with the main method that is used in flutter which is creating a widget class for each widget that you feel u should be defining a getter for it

it is bad practice because :

  1. when you are creating a widget with a function there is no element associated to that widget, and you are passing and using the main widget's context

  2. when you are creating a separate class for a widget you have the option to refactor it in lots of ways for example make it stateful or stateless without any hassle

  3. It is hard to debug when you define a widget in a separate class you will give a name to it, and it is easy to trace that widget inside the widget tree and debug it

and lots of other things, the described method is similar to the functional components in reacting there is a good discussion about this in the flutters GitHub page that some good programmers have participated you can see that for more knowledge about this topic here is the link : https://github.com/flutter/flutter/issues/19269

Share:
162
Sergey Shustikov
Author by

Sergey Shustikov

Android developer

Updated on December 29, 2022

Comments

  • Sergey Shustikov
    Sergey Shustikov over 1 year

    Today I faced the new code composing style in Flutter. Usually I'm using the next way to declare widget tree :

    This is a class that we have after create new flutter project(to save your time I publish only build method) :

    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
    
    

    The difference, that I seen today in some flutter project in github, to declare widgets as getters :

    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                textPushed,
                counterText,
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    
      Widget get textPushed {
        return Text(
          'You have pushed the button this many times:',
        );
      }
    
      Widget get counterText {
        return Text(
          '$_counter',
          style: Theme.of(context).textTheme.headline4,
        );
    

    I have never seen this approach of using getters for building widgets and I want to know is it a good practice or not and why?