Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in updating state without delay for refresh view #459

Closed
DJafari opened this issue Aug 5, 2019 · 1 comment
Closed

bug in updating state without delay for refresh view #459

DJafari opened this issue Aug 5, 2019 · 1 comment
Assignees
Labels
duplicate This issue or pull request already exists
Projects

Comments

@DJafari
Copy link

DJafari commented Aug 5, 2019

i have a problem in this library :

when i use this code :

import 'package:bloc/bloc.dart';

enum TestState {
  INIT, LOADING, LOCAL_SUCCESS, REMOTE_SUCCESS, FINISH
}

enum TestEvent {
  Get
}

class TestBloc extends Bloc<TestEvent, TestState> {
  @override
  TestState get initialState => TestState.INIT;

  @override
  Stream<TestState> mapEventToState(TestEvent event) async* {
    if(event == TestEvent.Get) {
      yield TestState.LOADING;
      yield TestState.LOCAL_SUCCESS;
      yield TestState.REMOTE_SUCCESS;
      yield TestState.FINISH;
    }
  }
}
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'test_bloc.dart';

class TestPage extends StatefulWidget {
  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  final bloc = TestBloc();

  @override
  void initState() {
    bloc.dispatch(TestEvent.Get);
    super.initState();
  }

  @override
  void dispose() {
    bloc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: BlocBuilder(
        bloc: bloc,
        builder: (context, state) {
          print(state);
          return Container();
        },
      ),
    );
  }
}

when i run app, my output is :

I/flutter ( 7180): TestState.INIT
I/flutter ( 7180): TestState.FINISH

but when i add some delay before states :

import 'package:bloc/bloc.dart';

enum TestState {
  INIT, LOADING, LOCAL_SUCCESS, REMOTE_SUCCESS, FINISH
}

enum TestEvent {
  Get
}

class TestBloc extends Bloc<TestEvent, TestState> {
  @override
  TestState get initialState => TestState.INIT;

  @override
  Stream<TestState> mapEventToState(TestEvent event) async* {
    if(event == TestEvent.Get) {
      yield TestState.LOADING;
      await Future.delayed(Duration(milliseconds: 100));
      yield TestState.LOCAL_SUCCESS;
      await Future.delayed(Duration(milliseconds: 100));
      yield TestState.REMOTE_SUCCESS;
      await Future.delayed(Duration(milliseconds: 100));
      yield TestState.FINISH;
    }
  }
}

output :

I/flutter ( 7180): TestState.INIT
I/flutter ( 7180): TestState.LOADING
I/flutter ( 7180): TestState.LOCAL_SUCCESS
I/flutter ( 7180): TestState.REMOTE_SUCCESS
I/flutter ( 7180): TestState.FINISH

note : this bug exists only in flutter package, because output of this :

bloc.state.listen((state) {
  print(state);
});

is ok

@felangel felangel self-assigned this Aug 6, 2019
@felangel felangel added the duplicate This issue or pull request already exists label Aug 6, 2019
@felangel
Copy link
Owner

felangel commented Aug 6, 2019

Hi @DJafari 👋
Thanks for opening an issue!

I believe this is a duplicate of #395.
TL;DR this is working as expected. You're simply yielding states faster than flutter can rebuild the UI. The same thing would happen if you used setState and changed the state back-to-back.

Hope that helps 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
bloc
  
Done
Development

No branches or pull requests

2 participants