-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question regarding best practices #1233
Comments
Hi 👋 You should always aim at providing your bloc at the lowest level possible(the lowest common widget), so only widgets that care about it could access it. As for your example, you should have the login bloc in your login page and home bloc in your home page, since they are dealing with 2 separate pages which don't seem to share anything between them. |
@RollyPeres , I have seen examples of providing a bloc required by child on the parent level, is it same as your approach? and when I need to use that bloc I use blocProvider in initState and close it in dispose method. |
You provide the bloc at the common parent level so that all children who care about it could access it. |
@RollyPeres , It shows warning in my vscode, which forces me to use init and dispose method. |
Please share a repo or gist which reproduces the issue your facing. |
@felangel @RollyPeres I don't have a code rn, but when using the approach you suggested it always gives warning to close the sink. That's why I am using the approach in which I close the bloc manually. |
@jaydangar it's very likely that you're facing a false positive from the close_sinks lint rule. You can see the open issue tracking at dart-lang/sdk#57882. For now, you can probably safely disable the Hope that helps and closing for now but feel free to comment with additional questions and I'm happy to continue the conversation 👍 |
Thanks @felangel , is it okay to stick with my approach instead of relying on closing the bloc automatically? And thanks for the response. |
As long as you're creating the blocs via BlocProvider you should be good and don't need to manually dispose them. Also make sure you're providing blocs as low as possible in the widget tree so that only the widgets that need access to the bloc can access it. |
Thanks @felangel got my answer. Amd really appreciate your efforts. |
@felangel I am still getting the following Warning. See the Image below. It seems like it's not getting disposed automatically.
|
Hi @jaydangar 👋 Check out #587 (comment) for more details. |
@felangel I understand, but I have a requirnment in which I need to send API call, which is through FetchCookListEvent() function. for that I need access to the bloc. So I declared global variable of that bloc and closed it in dispose method. |
You don’t need to dispose the bloc manually though. Either ignore the warning by adding |
@felangel Another thing I want to mention is that, I am getting error called The getter 'userNameField' was called on null, for the following code. Can you let me know why I am getting it? I have declared the bloc as suggested. I am declaring my bloc and using it for furthrer use, inside build method.
|
You should be accessing properties on the bloc state rather than the bloc itself. I would highly recommend reading through the documentation and in this case use BlocBuilder to access the usernameField. |
@felangel Actually I am using Form_Bloc library, which requires to provide bloc as a parameter. See the following code:
Here as you can see TextFieldBlocBuilder is a builder method, by which the widget gets updated. So, I have a different requirnment. Can you kindly let me know if there's any effective solution for this? I am adding a link to TextFieldBlocBuilder class Documentation. |
First of all big thank you for making "our" work really smooth by creating this library, I don't know how many headaches I have been through while learning streams, sinks and controllers. Now. while learning flutter_bloc I want to ask about best practice regarding bloc provider. I have 2 approaches.
My scenario :
Suppose I have 3 pages and I want to access bloc in HomePage Widget (grandchild).
-HomePage (grandchild)
First Approach :
I have created bloc for child and grandchild, lets call them LogInBloc and HomePageBloc, and to use them I am declaring them through multiBlocProvider in MainPage, and access them individually in LogInPage and HomePage class.
Second Approach :
I create provider for LogInBloc in MainPage Widget and then to access HomePageBloc I am using BlocProvider in LogInPage.
which one is the more common and best practice? are both equally suited?
The text was updated successfully, but these errors were encountered: