GithubHelp home page GithubHelp logo

Comments (9)

felangel avatar felangel commented on May 5, 2024

@QoLTech thanks for reporting this!

Can you please put together a super simple app that exhibits this behavior?

I'm unable to reproduce this with the following code:

import 'package:bloc/bloc.dart';

abstract class CounterEvent {}

class Increment extends CounterEvent {
  @override
  String toString() => 'Increment';
}

class Decrement extends CounterEvent {
  @override
  String toString() => 'Decrement';
}

class CounterBloc extends Bloc<CounterEvent, int> {
  @override
  int get initialState => 0;

  @override
  Stream<int> mapEventToState(int currentState, CounterEvent event) async* {
    if (event is Increment) {
      yield currentState + 1;
    }
    if (event is Decrement) {
      yield currentState - 1;
    }
  }
}

class SimpleBlocDelegate implements BlocDelegate {
  @override
  void onTransition(Transition transition) {
    print(transition.toString());
  }
}

void main() {
  BlocSupervisor().delegate = SimpleBlocDelegate();

  final counterBloc = CounterBloc();

  for (int i = 0; i < 100; i++) {
    counterBloc.dispatch(Increment());
  }
}

from bloc.

QoLTech avatar QoLTech commented on May 5, 2024

I've got my code that I'm currently working on here. It's slightly more complicated than a sample app, but I can easily reproduce the error.

After building and running, just hit connect and after connecting, you'll be on the messaging page. You'll have to use this to send messages. To reproduce, I just rapidly send messages with a single digit (1-9). When I do this, I see 1-9 printed to the console which is from the listener for messages and I only see an extra 9 which is from the Bloc mapEventToState.

Let me know if you need anything else.

from bloc.

felangel avatar felangel commented on May 5, 2024

@QoLTech I finally had a chance to take a look and the reason why this is happening is because you are overriding transform in the MessageBloc and debouncing. If you remove the transform override it will work as you expect.

from bloc.

felangel avatar felangel commented on May 5, 2024

In addition, I noticed that you have your own bloc_builder and bloc_provider. I'm not sure if this is intentional but if you want you can add flutter_bloc to your dependencies and you'll have access to BlocBuilder and BlocProvider without needed to copy the code into your own project.

from bloc.

QoLTech avatar QoLTech commented on May 5, 2024

@QoLTech I finally had a chance to take a look and the reason why this is happening is because you are overriding transform in the MessageBloc and debouncing. If you remove the transform override it will work as you expect.

That's great, thank you. Could you explain or link me to an explanation of how debouncing works?

In addition, I noticed that you have your own bloc_builder and bloc_provider. I'm not sure if this is intentional but if you want you can add flutter_bloc to your dependencies and you'll have access to BlocBuilder and BlocProvider without needed to copy the code into your own project.

Yes, I didn't realize that package was also on pub. I will make sure to add it as a dependency, rather than copy it into my own projects.

from bloc.

felangel avatar felangel commented on May 5, 2024

@QoLTech you can learn about denounce here.

Let me know if you have any other questions. 👍

from bloc.

QoLTech avatar QoLTech commented on May 5, 2024

@QoLTech you can learn about denounce here.

Let me know if you have any other questions.

At first I thought the time had something to do with it, but couldn't figure out what exactly. Can I ask what the benefit of something like this is? If a button gets tapped in quick succession? If so, why do it this way rather than checking the dispatched events for equality?

from bloc.

felangel avatar felangel commented on May 5, 2024

@QoLTech it’s useful if you’re implementing something like a search and you don’t want to make a network request after every key press because it will put unnecessary load on the backend.

You can debounce with a duration of something like 500 milliseconds and events that are dispatched less than 500 milliseconds apart won’t be processed. This allows you to only hit your backend after a user has stopped typing.

from bloc.

QoLTech avatar QoLTech commented on May 5, 2024

@QoLTech it’s useful if you’re implementing something like a search and you don’t want to make a network request after every key press because it will put unnecessary load on the backend.

You can debounce with a duration of something like 500 milliseconds and events that are dispatched less than 500 milliseconds apart won’t be processed. This allows you to only hit your backend after a user has stopped typing.

That's great, thank you.

from bloc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.