Using BLoC in Flutter - Usage in Stateful widgets vs Stateless Widgets - flutter

While using BLoC in flutter, whats the difference between defining the BlocBuilder / BlocListener in A Stateful widgets vs a Stateless widget ?
Can't we always use Stateless widgets as bloc takes care of rebuilding the widget for us ? Are there any usecases where you might want to use a Stateful widget instead ?
PS : I am experimenting with the flutter_bloc 1.0.0 package for flutter (https://pub.dev/packages/flutter_bloc).

You can use stateless weights throughout the app as any rebuilding can be handled by the builder methods in StreamBuilder or BlocBuilder. There is no requirement to do so, though it is generally recommended, but it might make sense to make some small widget stateful if you want a click to toggle some information or display the button that interacts with the bloc.

Related

When do we need to rebuild the StatelessWidget?

In the Flutter job interviews, sometimes I am asked how we can rebuild the StatelessWidget and the answer to this is to call the markNeedsBuild() method of the widget's Element or BuildContext.
So the question is, are there any cases when we need to rebuild a StatelessWidget and it's better to use a StatelessWidget instead of the StatefulWidget?
I found cases where people wanted to rebuild the StatelessWidget, such as How to invoke a rebuild of a stateless widget?, but in all these cases it was better to use StatefulWidget or state management instead.
The whole purpose of the StatelessWidget is not to rebuild.
If you require rebuild you have to use StatefullWidget.
I guess the interviewers are trying to trick you xD
Summary
Stateless Widget:
Stateless Widgets are static widgets.
They do not depend on any data change or any behavior change.
Stateless Widgets do not have a state, they will be rendered once and will not update themselves, but will only be updated when external data changes.
For Example: Text, Icon, RaisedButton are Stateless Widgets.
Stateful Widget:
Stateful Widgets are dynamic widgets.
They can be updated during runtime based on user action or data change.
Stateful Widgets have an internal state and can re-render if the input data changes or if Widget’s state changes.
For Example: Checkbox, Radio Button, Slider are Stateful Widgets

When using a ListView in flutter to retrieve a list of json objects from an API. Is it the right way to have an Stateful or Stateless Widget?

So say I have a simple app with 2 routes. The home route is profile and the second is items. So the idea is that when the user clicks in the item icon the app should move to that route and display all the user's items. The question is should this item Widget be Stateless or Stateful? Take in consideration that I expect the user to pull to refresh this Route and reload the api in the Listview.
It should definitely be a Stateful widget since you are expecting data from an API. A stateless widget can't change, you should only use a Stateless widget if the data on the page is static.
I know you already have an accepted answer which is correct, but wanted to add that generally if you use a state management solution like Provider, Bloc, GetX etc...they all provide widgets that will refresh a ListView without needing to be inside a stateful widget.
That will be a cleaner and more scalable solution than calling setState from within a stateful widget.
Its definitely an stateful widget because stateless widget can't change their state.

Why do we need stateless widgets if the same can be achieved by stateful widgets in flutter?

I am a newbie in the world of flutter, and I recently learned (or I think I did) about stateful and stateless widgets which is kind of the base for flutter widgets.
We use stateless widgets for things that are not redrawn on the display, (like text, button etc.) but stateful widgets can redraw themselves.
So my question is why do we need stateless widgets if stateful widgets can be used to draw the same kind of widgets that a stateless widget can?
Or is there any specific reasons to use stateless over stateful widgets in flutter? Or can we use stateful widgets all the time rather than stateless widgets which can draw content only once?
Thanks, and sorry if this is a stupid question.
EDIT
Well the question is not the difference between stateless and stateful.
I know the difference but what is the impact of using only stateful widgets since by using it we could also implement most of the things a stateless widget can do then why do we need stateless widgets?what's the importance of it in a flutter environment were most of the apps will be re-drawn time-to-time?
From their documentation:
Stateless widget are useful when the part of the user interface you are describing does not depend on anything other than the configuration information in the object itself and the BuildContext in which the widget is inflated. (= use when you don't need to "update the UI here").
Stateful widgets are more resource consuming and you always need to think about performance.
Here is more about this.
Push the state to the leaves. For example, if your page has a ticking
clock, rather than putting the state at the top of the page and
rebuilding the entire page each time the clock ticks, create a
dedicated clock widget that only updates itself.
Even more on this :)
I hope this answers your question.
Yes, StatefulWidget can rebuild. That happens typically when using Inheritedwidgets.
StatelessWidget exists to split a big widget tree into smaller reusable widgets.
You might think "but I can use StatefulWidget or functions for this". Which is true, but not exactly:
StatefulWidget comes with a huge boilerplate, which you do not need in that situation. So this just adds noise and makes your code less readable.
Functions cannot rebuild independently, nor to they have access to key and override ==. So they can be less performant or introduce bugs.
Every time we use a Stateful widget, a state object is created. If we use all Stateful widgets, there will be a lot of unnecessary state objects which will consume a lot of memory. So where we don't need to change the state, we should use the simpler one - the Stateless widget.
Another reason to use Stateless widgets over Stateful widgets is Stateful widget comes with a huge boilerplate and according to Flutter API Documentation using a bunch of nested Stateful widgets, passing data through all those build methods and constructors can get cumbersome.
This is what I understand ...
When you use a stateful widget and redraw it, all other widgets inside will be redrawn as well. So we try to use stateless widgets so as not to redraw the other widgets within them, but you know we generally need to change the data on the screen and it should happen inside a "single widget" and this widget should be the one stateful widget to use as little computing power as possible.
Now ... I guess you're thinking: "but what if i just use stateful widgets and don't redraw them?" well, as you know, when you use a stateful widget you have two classes: widget and state. I have gotten that when you declare a state you tell the phone to keep and save this state in memory whether the widget is redrawn or not, so you use the phone memory for no reason because you don't need to redraw its widget.
We should think about always using stateless widgets because they are lighter than stateful widgets and we should always make our stateful widgets the smallest in our application to redraw as few widgets as possible in the widget tree of the application.
I hope I helped a little.
Theoretically I think you can use stateful widgets every where, but your overall program would be heavier and would have a lot of redundancies.
That said, I don't think there is any advantage of using stateless widgets over stateful widgets.

Difference between Widget and Stateless widget

In the Flutter docs they say that a Widget is a mutable configuration (description) for an Element, which I have no problem understanding.
But then we have the stateless widget, which extends the Widget class, and must implement a build method. The two classes look like they do the same job, can any one explain the difference ?
Thanks.
Widget is just an interface. You're never gonna use it directly, and this class does absolutely nothing.
It exists just for the compiler to know that your program is type-safe.
every thing with flutter made with widgets there is no widget only it is Stateless widgets and statefull widgets
and the stateless is static and the other is dynamic
stateless for fixed layouts if you need any dynamic programming like(validation, requests, listeners for buttons and others..)
you need to use Statefull widgets
for more information see here..
difference between stateless and statefull widgets

Should screens be Stateless or Stateful?

I'm developing an app with lots of screens and pages. I've read somewhere that you should use Stateless Widgets whenever you can.
Why is that?
If I have a lot of screens, should those be stateless? And then the content inside be Stateful? Is it better to have both the screen and widgets inside being Stateful?
You should ask yourself some questions about the screen/page to decide if it will be Stateless or Stateful.
The most obvious, does it need to change state?
Do you need to call initState, didChangeDependencies or another lifecycle method?
Is a bad practice to make Stateful when not needed. A good idea might be to start always as Stateless widget and if needed you can change it to Stateful easily with Alt + Enter shortcut (Android Studio).
I always start by creating a Stateless Widget and work with it until I have to change something state. So I could quickly use `Alt-Enter/ Convert to a Statefull from Intellij/AS to change it to stateful. (doing the inverse is not so easy, so...).
Moreover, if you use Stateful widget with some async mechanism, like streams, you could build the widget once and use the streams to update the information you need, and it will not impact the performance of your app so much. But if you call setState many times, this can degrade your App, since for every setState the Widget tree will be rebuilt.
This article from the flutter docs shows interesting tips about handling state change in flutter apps: