I am trying to build a reddit clone app
and the error is when clicking on edit community in mod toolsenter image description here
i was building the edit community screen it was fine until i addedreturn ref.read(getCommunityByNameProvider(widget.name)).when( data: (community) => Scaffold(
the provider
final getCommunityByNameProvider = StreamProvider.family((ref, String name) {
return ref
.watch(communityControllerProvider.notifier)
.getCommunityByName(name);
});
Stream<Community> getCommunityByName(String name) {
return _communities.doc(name).snapshots().map(
(event) => Community.fromMap(event.data() as Map<String, dynamic>));
}
the respiratory
Stream<Community> getCommunityByName(String name) {
return _communityRepository.getCommunityByName(name);
}
data model
class Community {
final String name;
final String id;
final String banner;
final String avatar;
final List<String> members;
final List<String> mods
i use a similar method in the communities screen and it doesnt have any issues
I’m new to flutter , I have no idea what to provide , so if u need any more information please ask , I apologise for the inconvenience
2
Answers
As per the error
it is saying that you are trying to typecast
Null
asMap<String, dynamic>
check if
event.data()
is returning null in the below lineIt means
event.data()
isnull
.You have several solutions
1. Fix your firebase data
You have to find out in Firebase why
event.data()
isnull
(maybe the document doesn’t exist?). Once done, there shouldn’t be any issue anymore2. Support
null
Instead of casting directly to
Map<String, dynamic>
, make sure it is notnull
:3. Filter out the
null
s