BlocProvider.of <AuthhBloc>(上下文).add(ShowHome())未触发任何事件

时间:2020-08-30 19:13:40

标签: flutter dart bloc flutter-bloc

我已经使用VS代码上的可用扩展名创建了集团。我也尝试通过按钮触发事件,但是不起作用。它显示调试控制台中的默认状态和第一状态,即-“ AuthInitail”。每当我尝试触发任何新事件时,它都会显示相同的内容。我在下面提到代码-

auth_bloc.dart

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
part 'auth_event.dart';
part 'auth_state.dart';

class AuthBloc extends Bloc<AuthEvent, AuthState> {
  AuthBloc() : super(AuthInitial());

  @override
  Stream<AuthState> mapEventToState(
    AuthEvent event,
  ) async* {
    if(event is AppInit){
      yield AppStarting();
      startTime();
    }
    if(event is ShowHome){
      yield HomeState();
    }
    if(event is ShowAuth){
      yield AuthInitial();
    }
    if(event is ShowRegsiter){
      yield RegsiterInit();
    }
  }


  startTime() async {
    var _duration = new Duration(seconds: 3);
    return new Timer(_duration, navigationPage);
  }

  void navigationPage() {
    this.add(ShowHome());
  }
}

auth_event.dart


abstract class AuthEvent extends Equatable {
  const AuthEvent();

  @override
  List<Object> get props => [];
}
  class AppInit extends AuthEvent{}

  class ShowRegsiter extends AuthEvent{}

  class ShowAuth extends AuthEvent{}

  class ShowHome extends AuthEvent{}

auth_state.dart


abstract class AuthState extends Equatable {
  const AuthState();
  @override
  List<Object> get props => [];
}

class AppStarting extends AuthState{}

class AuthInitial extends AuthState {}

class AuthLoading extends AuthState{}

class AuthSuccess extends AuthState{
  final String message;
  AuthSuccess({@required this.message}):super();
}

class AuthError extends AuthState{
  final String message;
  AuthError({@required this.message}):super();
}
class HomeState extends AuthState{}

class LoggedIn extends AuthState{}

class NotLoggedIn extends AuthState{}

class RegsiterInit extends AuthState{}

class RegisterLoading extends AuthState{}

class RegsiterSuccess extends AuthState{
  final String message;
  RegsiterSuccess({@required this.message}):super();
}

class RegisterError extends AuthState{
  final String message;
  RegisterError({@required this.message}):super();
}

main.dart

import 'package:ecommerce/src/Auth/otp.dart';
import 'package:ecommerce/src/Auth/password.dart';
import 'package:ecommerce/src/Auth/signup.dart';
import 'package:ecommerce/src/ui/CartPage.dart';
import 'package:ecommerce/src/ui/homePage.dart';
import 'package:ecommerce/src/Auth/login.Dart';
import 'package:ecommerce/src/splash/splash_screens.dart';
import 'package:ecommerce/src/Constant/Constant.dart';
import 'package:flutter/material.dart';
import 'package:ecommerce/src/ui/profile.dart';
import 'package:flutter_bloc/flutter_bloc.dart';


void main(){
  final BlocProvider<AuthBloc> blocProvider = BlocProvider<AuthBloc>(
    create: (BuildContext cantext)=>AuthBloc(),
    child: MyApp(),
  );
  runApp(blocProvider);
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Demo',
      theme: ThemeData(
        fontFamily: 'Open',
        primaryColor: Colors.orange,
        accentColor: Colors.orange,
      ),
      routes: <String,WidgetBuilder>{
        SPLASH_SCREEN: (BuildContext context)=> AnimatedSplashScreen(),
        LOGIN_PAGE:(BuildContext context) => LoginPage(),
        OTP_PAGE:(BuildContext context)=> OTPPage(),
        PASSWORD_PAGE:(BuildContext context)=>LoginPWDPage(),
        SIGNUP_PAGE:(BuildContext context)=>SignupPage(),
        HOME_SCREEN: (BuildContext context)=> HomePage(),
        PROFILE_PAGE:(BuildContext context)=>ProfilePage(),
        CART_PAGE:(BuildContext context)=>CartPage()
      },
      home: BlocBuilder<AuthBloc,AuthState>(
        cubit: AuthBloc(),
        builder: (_,state){
          print(state.toString());
          if(state is AuthInitial){
            // return HomePage();
            return AnimatedSplashScreen(bloc: BlocProvider.of<AuthBloc>(context));
          }
          if(state is HomeState){
            return HomePage();
          }
          return CircularProgressIndicator();
        },
      )
    );
  }
}

问题- 我创建了一个函数startTime()来在3节之后触发一个事件,但是它不起作用。请帮我弄清楚

0 个答案:

没有答案