当我重新渲染页面时,所有状态都清晰可见,BLoC模式

时间:2019-04-17 16:20:23

标签: flutter bloc

我是新手,但遇到了问题。 为了存储我的应用程序的状态,我在Flutter-block库的帮助下使用了BLoC模式。 我的问题是,当我修改初始状态时,它已经被很好地修改了,但是一旦页面上发生操作,我所有的状态就会清除并返回到初始状态

在我的登录页面的密码字段下,您可以看到存储在我所在州的布尔。 一旦我单击页面上的小部件,状态便直接变为初始状态。

这是我的集团:

import 'package:bloc/bloc.dart';
import 'package:smartwork/src/events/Auth.event.dart';
import 'package:smartwork/src/repositories/Auth.repo.dart';
import 'package:smartwork/src/states/Auth.state.dart';

class AuthBloc extends Bloc<AuthEvent, AuthState> {
  void authRequest(email, password) {
    dispatch(AuthRequest(email, password));
  }

  @override
  AuthState get initialState => AuthState.initial();

  @override
  Stream<AuthState> mapEventToState(AuthEvent event) async* {
    if (event is AuthRequest) {
      var data;
      try {
        yield AuthState(isFetching: true);
        data = await AuthRepo().authRequest(event.email, event.password);
        yield AuthState(token: data["token"]);
        print(data["token"]);
      } catch (err) {
        yield AuthState(error: err);
      } finally {
        if (data["auth"] == true) {
          print("Test");
        }
        yield AuthState(isFetching: false);
      }
    }
  }
}

这是我的初始状态:

class AuthState {
  final String token;
  final bool isFetching;
  final String error;

  const AuthState({this.token, this.isFetching, this.error});

  factory AuthState.initial() => AuthState(token: "");
}

这是我的演示小部件,显示状态。isFetching布尔值:

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:smartwork/src/blocs/Auth.bloc.dart';
import 'package:smartwork/src/components/buttons/SubmitButton.dart';
import 'package:smartwork/src/states/Auth.state.dart';

class LoginForm extends StatefulWidget {
  @override
  _LoginFormState createState() => _LoginFormState();
}

class _LoginFormState extends State<LoginForm> {
  String _email, _password;
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Container(
        child: BlocBuilder(
            bloc: BlocProvider.of<AuthBloc>(context),
            builder: (context, AuthState state) {
              return Form(
                  key: _formKey,
                  child: new Column(children: <Widget>[
                    new TextFormField(
                      onSaved: (input) => {_email = input},
                      decoration: InputDecoration(
                        labelText: "Email :",
                      ),
                      keyboardType: TextInputType.emailAddress,
                    ),
                    new TextFormField(
                      obscureText: true,
                      onSaved: (input) => {_password = input},
                      decoration: InputDecoration(
                        labelText: "Password :",
                      ),
                    ),
                    Text("${state.isFetching}"),
                    Text("${state.token}"),
                    Padding(
                      padding: EdgeInsets.only(top: 20),
                      child: SubmitButton(onPressed: () {
                        _formKey.currentState.save();
                        BlocProvider.of<AuthBloc>(context)
                            .authRequest(_email, _password);
                      }),
                    )
                  ]));
            }));
  }
}

当您单击演示文稿页面的按钮之一时,isFetching状态立即变为空

预先感谢您的帮助!

0 个答案:

没有答案