Widgets vs helper methods | Decoding Flutter

Sdílet
Vložit
  • čas přidán 9. 12. 2021
  • Flutter developers are well-acquainted with widget build methods that grow, and grow… and grow. In this video, Craig Labenz compares two different ways to break up those unwieldy build methods; and how the best option may be different than what you originally suspected.
    See the differences in action in these DartPads:
    Managing rebuilds with nested const widgets → goo.gle/3yallg1
    Managing rebuilds with nested non-const widgets → goo.gle/3IyafpM
    Preventing errors with separate widgets → goo.gle/3dDrR5o
    Helper methods can create a requirement for Keys → goo.gle/31HlnQB
    Get started with Flutter → goo.gle/Flutter
    Learn more about the Dart Language → goo.gle/2YF9OCi
    Watch more Decoding Flutter episodes → goo.gle/DecodingFlutter
    Don’t miss an episode, subscribe to Flutter → goo.gle/FlutterYT
    #DecodingFlutter #Flutter #Developer
    product: Flutter - Development - User interface;
  • Věda a technologie

Komentáře • 167

  • @hawarhekmat1174
    @hawarhekmat1174 Před 2 lety +54

    thanks, that was so important to me i used helper methods, but from now i will use classes

  • @RandalLSchwartz
    @RandalLSchwartz Před 2 lety +41

    At least in VSC, the "refactor selected code to separate widget class" will notice variables defined out of scope, and automatically add them as parameters to the new class, providing the proper linkage at the original site. It stunned me the first time I tried that and it just worked. :)

    • @ykmnkmi
      @ykmnkmi Před 2 lety +1

      know your tooling first

    • @rodrickvy
      @rodrickvy Před 2 lety +2

      Ya , Intellij supports that too, would be better to have the class created in a new file though. Dont hv that yet

    • @Technochocolate
      @Technochocolate Před rokem +1

      I didn't know this existed! Thanks for mentioning this great tool!

    • @hrithikmishra1093
      @hrithikmishra1093 Před rokem +1

      This is so underrated, I felt like I could have saved so much of my time when I got to know about this.

  • @OKOK-hm2is
    @OKOK-hm2is Před 2 lety +10

    Greeeat video, it helped me a lot!
    As a novice in flutter I've started to use different classes for widgets right from the beginning as it felt logical for me. But watching more and more tutorials I've found out that people in them use helper method extensively and switched to that approach as it seemed easier to transfer data that way.
    Now, this video put thing in correct perspective and explains why it's not the best way

  • @danillubruk5775
    @danillubruk5775 Před 2 lety +1

    That was just soo helpful! As a new flutter developer, I just can't underestimate the value of such and advice!

  • @brendanlovelife
    @brendanlovelife Před 2 lety +138

    Key takeaway
    1. Use classes instead of helper functions
    2. Use const constructor wherever possible
    anymore ?

    • @syedmuzammilali8781
      @syedmuzammilali8781 Před 2 lety +1

      Thankyou so much !

    • @Lensbreak
      @Lensbreak Před 2 lety +9

      maybe make use of lint for the const constructor best practice

    • @uguur
      @uguur Před 2 lety

      this is not true sometimes helper functions more useful. you should watch this tutorial.

    • @longkhanh8544
      @longkhanh8544 Před 2 lety

      @@nielfollero5 why head to not go to?

    • @nielfollero5
      @nielfollero5 Před 2 lety

      @@longkhanh8544 It basically means the same

  • @myavuzokumus
    @myavuzokumus Před 5 měsíci +2

    I was editing the widgets today and I was confused about the difference and this video helped me, thanks!

  • @warperone
    @warperone Před 2 lety

    i like your explanation style and pace... slow and clear with simple examples , makes it really easy to absord

  • @HAROLHIGUERA
    @HAROLHIGUERA Před 2 lety +1

    I've been looking for this video for a while. Now I got it clear. Thanks👏

  • @AnirbanChakraborty94
    @AnirbanChakraborty94 Před 2 lety

    I was using the the helper method approach... But from now, I will use another class to wrap my Child widget build methods....
    Thanks for the insight... Much appreciated...

  • @srivats22
    @srivats22 Před 2 lety +1

    Thank you so much for this video. Was really eye opening and taught me something new!!

  • @SirJohn2024
    @SirJohn2024 Před 2 lety

    One of the most useful Decode to date... Kudos

  • @cholasimmons
    @cholasimmons Před 2 lety

    Been curious about this issue for a while now, you hit the nail on the head as always, thank you Google

  • @julius4858
    @julius4858 Před rokem

    Thanks! I’ve been approaching the limits of readability in my current project and the „refactor widgets“ todo has been getting more and more priority recently. Now I know what to look out for :)

  • @SXCRD
    @SXCRD Před 2 lety

    Very articulate and concise delivery!

  • @Aspiiire
    @Aspiiire Před 2 lety +11

    Great video! I want to add that naming your helpers or classes well is very important!

  • @kerandonovan7255
    @kerandonovan7255 Před 2 lety

    Great teaching style! Question I always wondered answered.

  • @user-jchjkitv77896
    @user-jchjkitv77896 Před 2 lety

    I just start to do it recently and my app performance got much better 🙂

  • @verry_codes
    @verry_codes Před 2 lety

    Thanks! Although I knew it already I needed to hear it from the flutter team again 😅!

  • @shourya-shikhar
    @shourya-shikhar Před 2 lety

    Solved the biggest confusion I was having. Thanks

  • @samerbrees3025
    @samerbrees3025 Před 2 lety +5

    Very nice explanation

  • @HermanPalmgren
    @HermanPalmgren Před 2 lety

    Great video! So many good things coming from Flutter

  • @hasansalim1868
    @hasansalim1868 Před 2 lety

    Thanks Craig. Very important video indeed.

  • @codeage1
    @codeage1 Před 2 lety

    Finally i got some arguments to use widgets 😍 love it 🥳

  • @ramzikhantouchi
    @ramzikhantouchi Před 2 lety +1

    Thank you. That was very important information

  • @jongwoolim616
    @jongwoolim616 Před 2 měsíci

    You are awesome. Always thankful for your explanation.

  • @lahirudx
    @lahirudx Před 2 lety +10

    I always used to use class constructors, because it makes code looks more readable and clearer for me. It's happy to hear that there are more valuable advantages too 😊.

  • @TheAbhidugar
    @TheAbhidugar Před 2 lety +1

    I also had thoughts about this topic. I started using separate classes for widgets but main problem I face with that is passing variables and callbacks are a bit challenging.

  • @AhmetZorer
    @AhmetZorer Před 2 lety +10

    Great Video! I have one minor addition. We can actually convert helper methods to generated widgets via functional_widget package.
    This way we get rid of both new class boilerplate and side effects of helper methods.

    • @thefamousdjx
      @thefamousdjx Před 2 lety +4

      Why use yet another package when you can just right click and refactor to a class automatically. Not sure what IDE you use but android studio does this out of the box

    • @AhmetZorer
      @AhmetZorer Před 2 lety

      @@thefamousdjx you have a very good point on this but adding new params or debugging properties makes classes longer and I have a feeling of writing boilerplate code unnecessarily. Therefore, I use this package :)

    • @avalanche2073
      @avalanche2073 Před 2 lety +3

      There are some annoyances that come with that package:
      - you have to run build_runner every time you open a project
      - files get generated next to classes and start to clutter your project
      - there is a way to tell build_runner to generate files in special folder 'generated',
      but moving files around project can break imports and you have to restart whole build_runner
      - doing 'extract widget' from context menu will still create class widget and you'll have to spend some time
      deleting code and replacing it with functional widget syntax
      - similarly, if you press F12 to see implementation of functional widget, you'll end up in generated file instead of
      actual function
      After summing all this things up I realized it's unfortunately not worth doing it to save couple of lines. It's a great idea though, and I hope there will be better solutions in the future.

    • @Apenschi
      @Apenschi Před 2 měsíci

      @@avalanche2073 And other devs need to know the package in order to understand your code

  • @heshansandeepa9471
    @heshansandeepa9471 Před rokem +1

    This is a extrenely valuable information. Thanks

    • @flutterdev
      @flutterdev  Před rokem +1

      We appreciate your positive feedback, Heshan 🙌

  • @mihirpaldhikar
    @mihirpaldhikar Před 2 lety +1

    Very precious information 🤯

  • @meron6913
    @meron6913 Před 2 lety

    super cool advice. i love it!

  • @luisrobertonavarro7581

    excellent exhibition, and I will not tire of asking for it. In Spanish, please. Thank you

  • @davidgaspar4772
    @davidgaspar4772 Před 2 lety

    Very good content, for everyone involved you are awesome. 👨🏻‍💻 🚀

  • @gameplaystrailers7797
    @gameplaystrailers7797 Před 2 lety

    So relieved I have had developed a habit of separating widgets

  • @aidarkhabibullin2906
    @aidarkhabibullin2906 Před 4 měsíci

    Great explanation!

  • @iamdragonkrys
    @iamdragonkrys Před 10 měsíci

    Super video. Great good practice.

  • @Thanho-nb6mt
    @Thanho-nb6mt Před 2 lety

    thanks, i understand more something from your video!!!!

  • @Vilmir
    @Vilmir Před 2 lety +4

    Ah crap, I need to rewrite a few things. Thanks! Backlog item added

  • @miriamramstudio3982
    @miriamramstudio3982 Před 2 lety +1

    Great video. Thanks

  • @xcoder5795
    @xcoder5795 Před 2 lety

    Good recommendations.

  • @sergey_molchanovsky
    @sergey_molchanovsky Před 2 lety +13

    Thank you for yet another great video! I knew about it, but it's always good to refresh in memory.
    The only thing I don't like is `const` keywords everywhere.
    Okay, we don't have that annoying `new` keyword, but we have to print `const` again and again. Is it really that difficult to make the compiler smarter, so it would automatically add consts in front of widgets? If it already has a lint, suggesting const, why not to get rid of `const` in front of constructor and make a compiler to add it when app is building?

    • @AdekMaulana
      @AdekMaulana Před 2 lety +3

      This couldn't agree more with you.
      Having to wrote `const` constantly is pretty annoying.

    • @chickenbloodmachine
      @chickenbloodmachine Před 2 lety +2

      Fully agree. If a constructor is already marked ‘const’, I don’t see why the compiler can’t quietly infer const wherever that constructor is invoked.

  • @maulord4
    @maulord4 Před rokem

    Yo me había preguntado eso. Gracias amigo

  • @mayankbhaisora2699
    @mayankbhaisora2699 Před rokem +1

    I never thought about performance and testability but I always preferred NewWidgets instead of helper methods as it looked more natural to do in platform like flutter.

  • @notanenglishperson9865
    @notanenglishperson9865 Před rokem +1

    I needed this

    • @flutterdev
      @flutterdev  Před rokem

      We're happy to hear that this was so helpful! Be sure to check out the links in the description for more tips and tutorials 🙂

  • @redz4110
    @redz4110 Před 2 lety +5

    I liked the video just for 3:49

  • @chichebem
    @chichebem Před 2 lety +1

    Very well said.

  • @mariapapageorgiou7446
    @mariapapageorgiou7446 Před rokem +3

    What about if you use a global state management package like GetX or state rebuilder? Doesn't that mean that only the reactive part of the widget tree is rebuilt on state change?

  • @guangyuchen7275
    @guangyuchen7275 Před 2 lety +3

    In some scenarios, a widget class can significantly out perform its corresponding helper function. In the scenarios where helper methods out performs widget classes, the performance advantage brought by helper functions over widget classes tends to be marginal. Plus other benefits of using widget class, if you want a no-brainer decision, using widget class seems a better bet.

  • @AJ-dy2bt
    @AJ-dy2bt Před 2 lety +2

    This is the case only for Stateful widgets or does it apply to completely Stateless widgets as well?

  • @user-zr4bj9zw9v
    @user-zr4bj9zw9v Před 2 lety +2

    Thank you for korean subtitle!!

  • @panyamred
    @panyamred Před 2 lety

    Nice tutorial

  • @into_your_armz977
    @into_your_armz977 Před 2 lety +1

    Nice video ❤️❤️

  • @san616mofo
    @san616mofo Před 2 lety

    Hi! Thanks for this video. What theme and font u use?)

  • @salahuddinshaikh7847
    @salahuddinshaikh7847 Před 2 lety

    I use methods to format code. So should I use classes for formatting also? I like it being seperated bcoz it makes me reading it easier.

  • @lydiabazova4332
    @lydiabazova4332 Před 2 lety

    Thank you for tehe video!

  • @bettercallshox
    @bettercallshox Před 2 lety

    Tried to apply this principle to create a countdown function but for some reason setting state in the countdown widget (which should only redraw the text every second) triggers a rebuild of its parent widget. Can't figure out why,

  • @hamzaibrahim895
    @hamzaibrahim895 Před 2 lety

    Thank you!

  • @zzzyyyxxx
    @zzzyyyxxx Před 2 lety +5

    Interesting that this video mentions Remi but doesn't mention his library `functional_widget` which basically turns a Widget function into a class via code generation. This is basically what I use. You get all the benefits of a class as mentioned in the video with the terseness of a Widget function.

    • @thefamousdjx
      @thefamousdjx Před 2 lety +1

      Because there's no need for a library when android studio already has this feature by simple right clicking and converting to class

  • @santiagomamani6111
    @santiagomamani6111 Před 2 lety +2

    Is there any scenario where helper methods would be a better choice than use a class?

  • @monokai6064
    @monokai6064 Před 2 lety

    great video

  • @sasha2209
    @sasha2209 Před 2 lety +1

    but what if we didnt have any setState inside the helper method ? is it still okay to use helper method ?

  • @chrisodonnell86
    @chrisodonnell86 Před 2 lety

    Geez wish someone told me this 3 years ago! I started with dozens of helper functions, slowly started using seperate widgets so the dart file wasn’t 2000 lines long. Still have a lot of refactoring to do 😅

  • @thefamousdjx
    @thefamousdjx Před 2 lety +5

    Classes are always great since you can isolate them to a separate file. As your app grows bigger and complex, it becomes easier to fix bugs when things are isolated. The logic is also easier to follow when code is broken down instead of having over 2000 lines in a single file

    • @Verrisin
      @Verrisin Před rokem

      top level functions are no different in this aspect

    • @pianoman16
      @pianoman16 Před 10 měsíci

      I couldn't disagree more. Lots of tiny source files makes the code a major pain to navigate through.

    • @alexandraee55
      @alexandraee55 Před 9 měsíci

      @@pianoman16 it's not painful if you give classes understandable names and use IDE's functions to navigate to type definition for example :)

  • @conceptcoder
    @conceptcoder Před 2 lety

    Thanks @Remi

  • @vatsaltanna5199
    @vatsaltanna5199 Před 2 lety +3

    So I feel there is one catch, what if we are not using setstate inside that sub widget ? than I guess there should not be any problem in performace and we can use function there instead of new widget. everyone do share your views if I missunderstood anything here.

  • @othnielonyoutube
    @othnielonyoutube Před 2 lety +3

    I never really liked too many lines of code in a single file. I believe that’s my unintentional love for class based widgets

  • @neijaywarner
    @neijaywarner Před 2 lety +1

    Nice!

  • @raizensangalang368
    @raizensangalang368 Před 2 lety

    I really love the intro music!
    can I have the full version? Hahaha

  • @thongm.truong459
    @thongm.truong459 Před rokem

    That's really nice

  • @KingLeoMessi
    @KingLeoMessi Před 2 lety +5

    I perfer using helper method when I am using bloc for state management but when using stateful widget and when using setstate seprate class is better.

    • @alexfokin4182
      @alexfokin4182 Před 2 lety +1

      What is the connection between the bloc and the methods that the widgets return? The essence of the video is different

    • @KingLeoMessi
      @KingLeoMessi Před 2 lety +1

      @@alexfokin4182 let me clear you. When we use bloc builder only the parts that need to change state is updated and that time we can use helper method as re render of whole ui is not done with the help of bloc builder as setstate is not used.

    • @laybunzz
      @laybunzz Před 2 lety +2

      @@KingLeoMessi Of course, everything returned by the BlocBuilder is re-rendered, so it's important to remember that helper methods embedded within that block of your code will incur all of these trade-offs :)

    • @tuyenvokhac7943
      @tuyenvokhac7943 Před 2 lety

      I agree with you. We can specify which widget need to be rebuilt by wrapping it with BlocBuilder, BlocSelector. Helper methods now make our code is easy to read and maintenance.

  • @HijabiCoder
    @HijabiCoder Před 2 lety +3

    Yay! I choose the right option then 🙃

  • @b_rabbit9774
    @b_rabbit9774 Před 2 lety

    is there a possibility that flutter will have a drag and drop method to create app in the future?

  • @brunoviana5349
    @brunoviana5349 Před 2 lety

    Amazing!

  • @nicollasoliveira9697
    @nicollasoliveira9697 Před rokem

    Thanks! These videos is really helpful!
    I was in doubt about to separate the widgets (like Text('Hello');) in local variables, if it is also a bad idea... Another doubt is if for example I have a Widget with a lot of params, can I pass these params to another Widget using its instance? Like: WidgetWithALotParams(SomeChildWidget(parent: this)); (then the SomeChildWidget will have access to all WidgetWithALotParams params)

    • @kirill4531
      @kirill4531 Před rokem +1

      create models for passing a lot of parameters

  • @__renesan
    @__renesan Před 2 lety

    Gracias ♥️

  • @ekopurnomo9221
    @ekopurnomo9221 Před 2 lety

    Thanks

  • @ganeshkgp8807
    @ganeshkgp8807 Před 2 lety

    thanks ☺️☺️☺️

  • @eugenekhumalo9671
    @eugenekhumalo9671 Před 2 lety

    Well this is helpful and I started doing this but hit some limitations (Either in my understanding or with Flutter)
    So I have a form that has a complex dropdown that I would rather seperate it.
    I still want to have access to values inside that dropdown for when I submit.
    With a helper function I have access to that variable and can read it when I'm ready to submit
    With a seperate class I don't have that link.
    Is there a way I can share this data?

    • @degroria
      @degroria Před 2 lety

      You can use name parameters to pass the values. Example: TextFiledWidget(value: value) something like this very easy..

  • @virtuososagar
    @virtuososagar Před 2 lety +1

    Amazing

  • @HungNguyen-pu1dv
    @HungNguyen-pu1dv Před 2 lety

    Where can I buy those dashes?

  • @ehsannazari1456
    @ehsannazari1456 Před 2 lety

    how about if we use getx?

  • @gideonogbonna8668
    @gideonogbonna8668 Před 2 lety +1

    I love the way he made us follow along 🤣.... I was just saying Yes, True, Ohhh That's true 😅

  • @jimmorrison2657
    @jimmorrison2657 Před 2 lety +2

    So, using classes is better than using helper methods.
    Ok, but is this a big difference?
    If it is not, I would prefer to use helper methods in general for smaller sub pieces of UI.
    Because using classes for these small things means you will have a huge amount of classes (all of which you have to try to name correctly), and a larger percentage of your codebase will be boiler plate.

  • @RamdanCode
    @RamdanCode Před 2 lety

    Thanks so much.
    Help me from the end of death on boilerplate coding.
    😁👍

  • @souravsuman636
    @souravsuman636 Před 2 lety +1

    is extracting widget in a variable is a good choice,??? Please reply .

    • @Trancecend
      @Trancecend Před 2 lety +1

      No it isn't. It goes against Flutter's philosophy of fast rebuilds.

  • @sarhansoft8563
    @sarhansoft8563 Před 2 měsíci

    I think the good benefit from classes is respectability in different widgets. But it look like more than that. anyway I always use that way.

  • @bluesilhouette6748
    @bluesilhouette6748 Před 2 lety +2

    So making a new class that extends stateless/full widget for your widget is better than making a separate method in the same class to return a widget(helper)?

    • @adamyong6766
      @adamyong6766 Před 2 lety

      It only helps in the example, as the favorite icon is a stateful widget than can uodate itself

    • @bluesilhouette6748
      @bluesilhouette6748 Před 2 lety +1

      @@adamyong6766 so if my widget is stateful. Better to make a separate class for it. But if stateless, its ok to use method(helper)?

    • @mivoligo
      @mivoligo Před 2 lety +1

      @@bluesilhouette6748 It doesn't matter if the widget is stateless or stateful. It's always better to use widgets than helper methods from the performance point of view.

    • @guangyuchen7275
      @guangyuchen7275 Před 2 lety

      @@mivoligo "always" is a very strong argument. Any supporting data?

    • @mivoligo
      @mivoligo Před 2 lety +1

      @@guangyuchen7275 sorry, I'm sure I've used the word "always" incorrectly. There must be a way to worsen performance using widgets. :) I just wanted to emphasize that it doesn't matter is the widget is stateful or stateless. Flutter is optimized to use widgets and not functions returning widgets.

  • @nameisvamshi5129
    @nameisvamshi5129 Před 2 lety

    Though this is not the right platform to ask...I am struggling in building an android app with flutter.. apk is not building.. There is an error in build.gradle file.. It says
    could not resolve properties..even stack overflow could not resolve my query

  • @programingtales7514
    @programingtales7514 Před rokem

    Why not use stateful builder with the helper widget?

  • @azmanbarraquias6707
    @azmanbarraquias6707 Před 12 dny

    the link is Error: Page not found, Please update.

  • @paljain01
    @paljain01 Před 2 lety

    this is the first time i heard about helper method.

  • @saqqara6361
    @saqqara6361 Před 2 lety

    Great!

  • @hassan_codes
    @hassan_codes Před 2 lety +3

    I've always used separate widgets. If I need to use a callback, I declare a property for that

  • @butbutmybutt
    @butbutmybutt Před 2 lety +1

    Its also bad to debug because the helper method is not visible in the debug widget tree

  • @rvb6516
    @rvb6516 Před 2 lety +3

    Rémi 🥖 🇫🇷 rocks 🥇💪😁

  • @a.belenky4490
    @a.belenky4490 Před rokem

    Minimizing dropped frames is nice but the real benefit is the power consumption. The vast majority of devices can run resource intensive apps effortlessly, but the power consumption is greatly affected. Making efficient apps is more for the battery life than for performance nowadays (although it's always good having faster apps as well)

  • @AnonymousAnonymous-nr8su

    It is sad that const won't work with any localisation approach and most of the endpoint widgets in the widget tree brunches are Text.

  • @caroby7658
    @caroby7658 Před 2 lety

    what about StatefulBuilder?

  • @hassan_codes
    @hassan_codes Před 2 lety +9

    I've always used separate widgets without knowing all these advantages.

  • @mobilemon5032
    @mobilemon5032 Před 2 lety +1

    i wish there was some way around this. helper methods make things so much easier
    if i have to create classes i have to pass in parameters and all code is more verbose

    • @ivankasalo2738
      @ivankasalo2738 Před rokem

      But usually helper methods require parameters too. Like context etc...