skip to Main Content

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


  1. As per the error
    it is saying that you are trying to typecast Null as Map<String, dynamic>

    check if event.data() is returning null in the below line

    event.data() as Map<String, dynamic>
    
    Login or Signup to reply.
  2. It means event.data() is null.

    You have several solutions

    1. Fix your firebase data

    You have to find out in Firebase why event.data() is null (maybe the document doesn’t exist?). Once done, there shouldn’t be any issue anymore

    2. Support null

    Instead of casting directly to Map<String, dynamic>, make sure it is not null:

    Stream<Community?> getCommunityByName(String name) {
      return _communities.doc(name).snapshots().map(
        (event) {
          if (event.data() == null) return `null`;
          return Community.fromMap(event.data() as Map<String, dynamic>));
      }
    }
    

    3. Filter out the nulls

    Stream<Community> getCommunityByName(String name) {
      return _communities.doc(name).snapshots().where((even) => even.data() != null).map(
        (event) => Community.fromMap(event.data() as Map<String, dynamic>),
      );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search