断言失败:'userRepository!= null':不正确

时间:2019-05-15 13:18:58

标签: dart flutter bloc

在使用Flutter的简单实现应用程序中,我正在使用Bloc,并且我试图将此构造函数用作:

class SimpleBlocDelegate extends BlocDelegate {
  @override
  void onEvent(Bloc bloc, Object event) {
    super.onEvent(bloc, event);
    print(event);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print(transition);
  }

  @override
  void onError(Bloc bloc, Object error, StackTrace stacktrace) {
    super.onError(bloc, error, stacktrace);
    print(error);
  }
}

void main() {
  BlocSupervisor().delegate = SimpleBlocDelegate();
  runApp(App(userRepository: UserRepository()));
}

class App extends StatefulWidget {
  final UserRepository userRepository;

  App({Key key, @required this.userRepository}) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  AuthenticationBloc _authenticationBloc;

  UserRepository get _userRepository => widget.userRepository;

  @override
  void initState() {
    _authenticationBloc = AuthenticationBloc(userRepository: _userRepository);
    _authenticationBloc.dispatch(AppStarted());
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    return BlocProvider<AuthenticationBloc>(
      bloc: _authenticationBloc,
      child: MaterialApp(
        home: BlocBuilder<AuthenticationEvent, AuthenticationStates>(
          bloc: _authenticationBloc,
          builder: (BuildContext context, AuthenticationStates state) {
            if (state is AuthenticationUninitialized) {
              return SplashScreen();
            } if (state is AuthenticationUnauthenticated) {
              return LoginScreen();
            } if (state is AuthenticationAuthenticated) {
              return Scaffold(
                  body: Center(child: Text('AuthenticationAuthenticated')));
            } if (state is AuthenticationLoading) {
              return Scaffold(
                  body: Center(child: Text('AuthenticationLoading')));
            }
          },
        ),
      ),
    );
  }
}
class LoginScreen extends StatefulWidget {
  final UserRepository userRepository;

  LoginScreen({Key key, @required this.userRepository})
        /* I GET ERROR WITH BELOW LINE */
      : assert(userRepository != null),
        super(key: key);

  State<LoginScreen> createState() {
    return _LoginScreenState();
  }
}

class _LoginScreenState extends State<LoginScreen> {
  AuthenticationBloc _authenticationBloc;

  UserRepository get _userRepository => widget.userRepository;
  LoginBloc _loginBloc;

  var _start = Offset(1, 0);
  var _end = Offset.zero;

  @override
  void initState() {
    _authenticationBloc = BlocProvider.of<AuthenticationBloc>(context);
    _loginBloc = LoginBloc(
        userRepository: _userRepository,
        authenticationBloc: _authenticationBloc);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: LoginForm(
        loginBloc: _loginBloc,
        userRepository: _userRepository,
      ),
    );
  }
}

在手机上安装应用程序后,出现此错误:

Failed assertion: line 14 pos 16:
I/flutter (12689): 'userRepository != null': is not true.

我无法解决

UserRepository类:

class UserRepository {
  final successCode = 200;

  Future<UserInfo> authenticate({
    @required String username,
    @required String password,
  }) async {
    final response = await http.get(
        Constants.url +
            "/api/v1/getPersons?username=$username&password=$password",
        headers: {"Content-Type": "application/json"});
    final responseString = jsonDecode(response.body);
    if (response.statusCode == successCode) {
      print(responseString);
      return UserInfo.fromJson(responseString);
    } else {
      throw Exception('failed to get information');
    }
  }
  ...
}

LoginBloc类:

class LoginBloc extends Bloc<LoginEvent, LoginState> {
  final UserRepository userRepository;
  final AuthenticationBloc authenticationBloc;

  LoginBloc({@required this.userRepository, @required this.authenticationBloc});

  @override
  LoginState get initialState => LoginInitial();

  @override
  Stream<LoginState> mapEventToState(LoginEvent event) async* {
    if(event is LoginButtonPressed){
     try{
       yield LoginLoading();

       await userRepository.authenticate(
           username:event.username,
           password:event.password
       );

       authenticationBloc.dispatch(LoggedIn(username: event.username,password: event.password));

       yield LoginInitial();
     }catch(error){
       LoginFailure(errorMessage: error.toString());
     }
    }
  }
}

LoginState类:

class LoginState extends Equatable{
  LoginState([List props = const[]]):super(props);
}

class LoginInitial extends LoginState{
  @override
  String toString()=>'LoginInitial';
}

class LoginLoading extends LoginState{
  @override
  String toString()=>'LoginLoading';
}

class LoginFailure extends LoginState{
  final String errorMessage;
  LoginFailure({this.errorMessage}):super([errorMessage]);

  @override
  String toString()=>'LoginFailure';
}

然后是LoginEvent类:

class LoginEvent extends Equatable {
  LoginEvent([List props = const []]) : super(props);
}

class LoginButtonPressed extends LoginEvent {
  final String username;
  final String password;

  LoginButtonPressed({@required this.username, @required this.password})
      : super([username, password]);

  @override
  String toString()=>'LoginButtonPressed {username:$username, password:$password}';
}

1 个答案:

答案 0 :(得分:1)

每次创建LoginScreen小部件或LoginBloc时,都要通过userRepository