felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.84k stars 3.4k forks source link

Basic issue with repository instantiation #1797

Closed ywaz closed 4 years ago

ywaz commented 4 years ago

Describe the bug The app is a simple messenger, using firestore. After setting the message event/state/bloc and also the repository that handles the message sending/fetching I instantiated the repository in main() however, the following error is returned:

'package:pingMe/bloc/bloc.dart': Failed assertion: line 12 pos 51: 'repository!=null': is not true.

I believe the solution is really straightforward, but I had a hard time to figure it out since I am new to this pattern.

Repository: image

bloc:

image

main: image

message event and state:

image

image

Many thanks in advance

Younes

narcodico commented 4 years ago

Hi @ywaz 👋

Looks like you're not passing the repository to your bloc in your BlocProvider creation. create: (context) => MessageBloc(repository: yourRepositoryInstance)

ywaz commented 4 years ago

Hi @narcodico ,

Thank you for your reply.

I see your point, but in the main SS (that was incomplete in my first message) I am intiializing my MessageBloc in the initstate and only used BlocBuilder, which dosn't contain bloc property anymore as you can see here:

image

If I use BlocProvider, is the instantiation of MessageBloc in the initstate no longer needed?

the blocprovider doesn't contain neither the bloc property as shown in the weather app example: https://medium.com/flutter-community/weather-app-with-flutter-bloc-e24a7253340d

thank you in advance.

Younes

narcodico commented 4 years ago

Please share a minimal github repo with your issue, your screenshots are incomplete.

ywaz commented 4 years ago

yeah sure,

just pushed it : https://github.com/ywaz/PingMe/tree/master

Thank you.

narcodico commented 4 years ago

I've open a PR with fixes and improved coding practices.

Your initial code wasn't working because you had at some point in one of the constructors {@required repository} instead of {@required this.repository} and also you had no BlocProvider<MessageBloc> to provide an instance of MessageBloc to your widget tree. The quickest fix would have been to specify in your BlocBuilder cubit: _msgBloc.