简体   繁体   English

Flutter 应用程序有时卡在启动画面上

[英]Flutter app stuck sometimes on the Splash screen

The app freezes on Android and iOS on the Splash screen when the app is not used for some time, like multiple days, or sometimes when I resume the recent apps, instead of opening the app on the icon tap.当应用程序一段时间未使用时(例如多天)或有时当我恢复最近的应用程序时,应用程序会在启动屏幕上的 Android 和 iOS 处冻结,而不是在图标点击时打开应用程序。

It just displays a splash screen with the app being black (since I have the dark theme) and an app icon and it sticks there.它只是显示一个启动画面,应用程序是黑色的(因为我有深色主题)和一个应用程序图标,它会粘在那里。

The only logic that I have is to init Firebase and to check if user is logged in.我唯一的逻辑是初始化 Firebase 并检查用户是否登录。

Here is the main.dart:这是 main.dart:

///Receive message when app is in background solution for on message
Future<void> backgroundHandler(RemoteMessage message) async {
  print('Handling a background message ${message.messageId}');
  print('Content of message: ' + message.toString());
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  configureInjections();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  FirebaseMessaging.onBackgroundMessage(backgroundHandler);
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) => MultiBlocProvider(
        providers: [
          BlocProvider(create: (context) => getIt<SplashScreenCubit>()),
          BlocProvider(create: (context) => getIt<HomeCubit>()),
          BlocProvider(create: (context) => getIt<LogInCubit>()),
          BlocProvider(create: (context) => getIt<SettingsCubit>())
        ],
        child: OverlaySupport(
          child: MaterialApp(
            debugShowCheckedModeBanner: false,
            title: 'SAY',
            theme: appTheme,
            home: SplashScreenPage(),
          ),
        ),
      );
}

Here is the Splash page:这是启动页面:

class SplashScreenPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => getIt<SplashScreenCubit>()..isUserAuthenticated(),
      child: BlocConsumer<SplashScreenCubit, SplashScreenState>(
          listener: (context, state) {
            if (state is Authenticated) {
              PageNavigator.navigateAndRemoveHistory(context, HomePage());
            }
            if (state is Unauthenticated) {
              PageNavigator.navigateAndRemoveHistory(context, LogInPage());
            }
            if (state is SplashError) {
              PageNavigator.navigateAndRemoveHistory(context, LogInPage());
            }
          },
          builder: (context, state) => SplashScreenContent()),
    );
  }
}

class SplashScreenContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) => Scaffold(
        body: Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          color: AppColors.primaryColor,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text('SAY', style: Theme.of(context).textTheme.headline1),
                    Text('Together we make it better!',
                        style: Theme.of(context)
                            .textTheme
                            .headline6!
                            .copyWith(color: AppColors.whiteColor),
                        textAlign: TextAlign.center),
                  ],
                ),
              ),
              Expanded(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [Image.asset(SvgIcons.splashScreenLogo)],
                ),
              )
            ],
          ),
        ),
      );
}

Here is the cubit:这是肘:

@injectable
class SplashScreenCubit extends Cubit<SplashScreenState> {
  final IsUserAuthenticatedUseCase _isUserAuthenticatedUseCase;

  SplashScreenCubit(this._isUserAuthenticatedUseCase) : super(Initial());

  Future<void> isUserAuthenticated() async {
    print("Called isUserAuthenticated on the splash screen");
    try {
      var isUserAuthenticated = await _isUserAuthenticatedUseCase.execute(NoParams());
      return isUserAuthenticated ? emit(Authenticated()) : emit(Unauthenticated());
    } catch (e) {
      emit(Unauthenticated());
    }
  }
}

Here is the call to the use case and repository:这是对用例和存储库的调用:

  @override
  Future<bool> isAuthenticated() {
    try {
      var currentUser = _firebaseAuth.currentUser;
      return Future.value(currentUser != null && currentUser.emailVerified);
    } catch (e) {
      return Future.value(false);
    }
  }

Okay try this in your main.dart .好的,在你的main.dart试试这个。 First I'm gonna use StreamBuilder and the Firebase method userChanges to check the auth state :首先,我将使用StreamBuilderFirebase方法userChanges来检查auth state

home: StreamBuilder(
          stream: FirebaseAuth.instance.userChanges(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.active) {
              if (snapshot.hasData) {
                return HomePage();
              } else if (snapshot.hasError) {
                return Center(
                  child: Text('${snapshot.error}'),
                );
              }
            }
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            return LogInPage();
          },
        ), 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何摆脱控制台警告:不推荐使用 expo-app-loading 以支持 expo-splash-screen? - How to I get rid of Console Warning: expo-app-loading is deprecated in favour of expo-splash-screen? 添加 firebase crashlytics 后,React 本机应用程序卡在启动屏幕上? - React native App get stuck at launch screen after adding firebase crashlytics? 为什么我在托管 Flutter 应用程序时出现灰屏? - Why do I get grey screen when hosting Flutter app? 设置 App Check 时出现白屏 Flutter Web - White Screen while setting App Check for Flutter Web 无法使用页面路由在颤振应用程序上加载另一个屏幕 - Not able to load another screen on flutter app using page routing FirebaseMessaging:应用程序在屏幕锁定或其他应用程序打开时不显示通知 iOS Flutter - FirebaseMessaging: App not showing notification when screen is locked or some other app is open iOS Flutter 卡在 Flutter Apple 登录凭据过程 - Stuck on Flutter Apple Login credential process 即使使用也不会显示启动画面<intent-filter>在里面</intent-filter> - Splash screen is not getting displayed even using <intent-filter> in it Flutter:主屏幕上 iOS 应用程序图标上的通知徽章计数器未显示(使用 FCM) - Flutter: The notification badge counter on the iOS app icon on the home screen is not showing up (using FCM) Firebase 数据未显示在 flutter 应用程序中 - Firebase data not displaying in flutter app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM