Flutter BloC not refresh my view (counter app)

后端 未结 3 1367
执笔经年
执笔经年 2021-01-16 03:37

I\'m trying BloC library with Counter test app but I have a little problem on my view when I use object to increment/decrement my counter. MyObject.MyProperty increment or d

3条回答
  •  不思量自难忘°
    2021-01-16 04:06

    Just posting another possible solution for future visitors.

    If you do yield for the same state twice. Your UI won't update.

    For example if you have already triggered ShowDataToUser(data) from your Bloc and now after another operation you are yielding ShowDataToUser(data) again, your UI won't update.

    A quick hack around this would be to just create a DummyState() in your Bloc and yield DummyState() everytime before you yield ShowDataToUser(data)

    Another solution apart from the quick hack around is to define a way for dart to compare and equate a class, cos currently by default ShowDataToUser is the same class, but its contents are different, but Dart doesn't know how to evaluate the content and compare them.

    Enters Equatable

    For this, you need to use the package : https://pub.dev/packages/equatable

    Now you have to extend your Bloc states with Equtable like:

    class DummyState extends Equatable {}
    

    Now, if your Bloc States are extending Equatable then you must override the function:

    @override
      List get props => [name];
    
    
    

    Here name is the property/field name you want to compare when comparing two same classes.

    This function tells your Bloc, how to differentiate between two same states (which might have different values in them).

    提交回复
    热议问题