I am trying to understand what impact does the lazy flag have. And what are the considerations for setting it as true or false?
When it is true, the Bloc or Cubit should be created lazily, that means, it is not created by the provider until the first request of accessing the Bloc such as context.read<MyBloc>
You can set lazy true when initializing is costly, or it has some side effects, i.e a Restful API called (it is not needed to call the API until user do some action and lazily created the Bloc) on otherside you can set lazy as false for prefetching data
As the docs say:
By default,
Create
is called only when the instance is accessed. To override this behavior, setlazy
to false.
Therefore, the impact is when Create
will be called, and thus when (if at all) your your BLoC is created. By default, lazy
is true
, so if you never access the instance of the BLoC, it will not be created. Conversely, when lazy
is false
, the Create
method will be called instantly when the Widget is built.
Behind the scenes, the value is passed to the underlying InheritedProvider.
© 2022 - 2024 — McMap. All rights reserved.
lazy
is true by default. – Chemarin