skip to Main Content

I’m creating an app with firebase as a database. After sending data to firebase, app screen should pop out for that I had bloclistener on the screen but after sending the data to firestore database, nothing is happening, flow is stopped after coming to loaded state in bloc file why? check my code so that you will know. I can see my data in firebase but it is not popping out because flow is not coming to listener.

  state:
    class SampletestInitial extends SampletestState {
  @override
  List<Object> get props => [];
}

class SampletestLoaded extends SampletestState {
  SampletestLoaded();

  @override
  List<Object> get props => [];
}

class SampletestError extends SampletestState {
  final error;

  SampletestError({required this.error});

  @override
  List<Object> get props => [error];
}

    
    bloc:
   class SampletestBloc extends Bloc<SampletestEvent, SampletestState> {
  SampletestBloc() : super(SampletestInitial()) {
    on<SampletestPostData>((event, emit) async {
      emit(SampletestInitial());
      try {
        await Repo().sampleTesting(event.des);
        emit(SampletestLoaded());
      } catch (e) {
        emit(SampletestError(error: e.toString()));
        print(e);
      }
    });
  }
}
    
    Repo: ---- Firebase post data
    Future<void> sampleTesting(String des) async {
    final docTicket = FirebaseFirestore.instance.collection('sample').doc();
    final json = {'Same': des};
    await docTicket.set(json);
  }
    
    TicketScreen:
 //After clicking the button ---
    BlocProvider<SampletestBloc>.value(
      value: BlocProvider.of<SampletestBloc>(context, listen: false)
        ..add(SampletestPostData(description.text)),
      child: BlocListener<SampletestBloc, SampletestState>(
        listener: (context, state) {
          if (state is SampletestLoaded) {
            Navigator.pop(context);
            print("Popped out");
          }
        },
      ),
  );

2

Answers


  1. im not sure but i think that you have the same hash of:

    AllData? data;
    

    try to remove AllData? data; and create new data variable so you can be sure that you has a new hash code every time you call createTicket method;

    final AllData data = await repo.createTicket(AllData(
    
    Login or Signup to reply.
  2. Check your AllData class properties.

    BLoC will not show a new state if it not unique.
    You need to check whether all fields of the AllData class are specified in the props field.

    And check your BlocProvider. For what you set listen: false ?

    BlocProvider.of<SampletestBloc>(context, listen: false)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search