Bloc: Cubit - consider using StackTrace to log the calling method ?

Created on 25 Oct 2020  路  4Comments  路  Source: felangel/bloc

The lack of logging info about the 'event' (the method) that called emit is documented as a downside of Cubit when compared to Bloc,
but have you considered using StackTrace.current (or at least have an optional argument) inside of Cubit.emit ?

  void emit(State state, [String methodName]) {
    (...)
    final transition = Transition(
        currentState: state,
        event: methodName ?? StackTrace.current.toString().split('\n')[1],
        nextState: nextState,
      )

that currently prints (...)event: #1 CounterCubit.increment (file:///Users/ben/Workspaces/Flutter/bloc/packages/bloc/example/main.dart:100:23)(...)
but could be further parsed to look like CounterCubit.increment():100:23

this would have to be benchmarked, but it would deprecate the Change class, and there would be one less downside to Cubit ;-)

question

All 4 comments

Hi @benoitjadinon 馃憢
Thanks for opening an issue!

I did originally look into this but it performs very poorly and does not work in release builds afaik so I don鈥檛 think it鈥檚 a valid solution. Thanks for the suggestion though and let me know what you think 馃憤

right, I just remembered it only works in debug mode too, sorry.
what about the optional argument ? wouldn't cost much, and if people don't add it, the transition just doesn't log it ?
it would be ugly, and hardcoded, and probably wouldn't change after a refactor, but still better than nothing, right ?

Hi @benoitjadinon 馃憢

I personally feel that people should be using cubits when they don't care about the source of change, but only about the end result, which is the state. If you care about who triggered those states then bloc should be used instead.

I agree with @narcodico. Introducing an optional argument for the method name will introduce a lot of additional complexity -- will the Change class also need an optional event? Then what will be the difference between Transition and Change? Should we combine them at that point? I think it introduces more problems than it solves, unfortunately 馃槥

Was this page helpful?
0 / 5 - 0 ratings