0

I have a problem with understanding one behaviour in BLoC pattern. Using flutter_bloc plugin

I made a project based on this one: https://felangel.github.io/bloc/#/flutterlogintutorial

Nothing special. I have two pages with buttons to navigate between them. So if i go to the second page and then back to login page and focus on any input field i see one extra output(' state');

Step-by-step:

  1. Start the app -> flutter debug console: LoginInitial
  2. Press 'Second Page'
  3. Press 'Login Page'-> flutter debug console: LoginInitial
  4. Focus on any input field -> flutter debug console: LoginInitial

Any ideas why is this happening on the 4th step?

Login page:

Widget build(BuildContext context) {
return BlocBuilder<LoginEvent, LoginState>(
  bloc: _loginBloc,
  builder: (
    BuildContext context,
    LoginState state,
  ) {
    print('  $state');
    if (state is LoginFailure) {
      _onWidgetDidBuild(() {
        Scaffold.of(context).showSnackBar(
          SnackBar(
            content: Text('${state.error}'),
            backgroundColor: Colors.red,
          ),
        );
      });
    }

    return Form(
      child: Column(
        children: [
          TextFormField(
            decoration: InputDecoration(labelText: 'username'),
            controller: _usernameController,
          ),
          TextFormField(
            decoration: InputDecoration(labelText: 'password'),
            controller: _passwordController,
            obscureText: true,
          ),
          RaisedButton(
            onPressed:
                state is! LoginLoading ? _onLoginButtonPressed : null,
            child: Text('Login'),
          ),
          Container(
            child:
                state is LoginLoading ? CircularProgressIndicator() : null,
          ),
        ],
      ),
    );
  },
);

}

Full project: https://github.com/ViktorKirjanov/flutter_login_issue

Stellar Creed
  • 384
  • 5
  • 14
  • This is not a problem. See https://stackoverflow.com/questions/52249578/how-to-deal-with-unwanted-widget-build – Rémi Rousselet Feb 18 '19 at 13:47
  • can you please provide a code for this case. const and final dont work – Stellar Creed Feb 18 '19 at 14:55
  • It's totally ok, your views have to change sizes/colors/etc according to focus event and re-draw. Just make sure that the fact of rebuild does not launch any heavy tasks. Events should, rebuild - should not. Think of building process as something that should be able to run 60 times a second without any side effects, and your views should be ready for such conditions. If you fail with that - you have bad architecture design. "The fact your widget is not being rebuilt 60 times/sec is just an optimization." – Buffer Underflow Feb 18 '19 at 22:33

0 Answers0