我已经编写了一个使用 Bloc 模式和 GetIt 包的 Flutter 应用程序。奇怪的是,该应用程序在 Android 设备上运行良好,但在 iOS 模拟器上,它只显示黑屏...
这里是我的代码:
main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
AppInitializer.init();
runApp(App());
}
app_initializer.dart
import 'package:authentication_repository/authentication_repository.dart';
import 'package:get_it/get_it.dart';
import 'package:techniciens/blocs/blocs.dart';
import 'package:user_repository/user_repository.dart';
class AppInitializer {
static final getIt = GetIt.instance;
static void init() {
_registerSingletons();
_registerBlocSingletons();
}
static void _registerSingletons() {
getIt.registerSingleton(AuthenticationRepository()..user.first);
getIt.registerLazySingleton<UserRepository>(() => UserRepository());
}
static void _registerBlocSingletons() {
getIt.registerLazySingleton<AuthenticationBloc>(() => AuthenticationBloc(
authenticationRepository:
GetIt.instance.get<AuthenticationRepository>()));
getIt.registerLazySingleton<SettingsFormCubit>(() => SettingsFormCubit());
getIt.registerLazySingleton<UserInfoCubit>(() => UserInfoCubit());
}
}
app.dart
import 'package:authentication_repository/authentication_repository.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:overlay_support/overlay_support.dart';
import 'package:techniciens/blocs/blocs.dart';
import 'package:techniciens/core/constants/constants.dart';
import 'package:techniciens/screens/screens.dart';
import 'package:get_it/get_it.dart';
import 'package:user_repository/user_repository.dart';
import 'core/theme/app_theme.dart';
class App extends StatelessWidget {
final AuthenticationRepository _authenticationRepository =
GetIt.instance.get<AuthenticationRepository>();
final UserRepository _userRepository = GetIt.instance.get<UserRepository>();
final AuthenticationBloc _authenticationBloc =
GetIt.instance.get<AuthenticationBloc>();
App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MultiRepositoryProvider(
providers: [
RepositoryProvider.value(value: _authenticationRepository),
RepositoryProvider.value(value: _userRepository),
],
child: BlocProvider<AuthenticationBloc>.value(
value: _authenticationBloc,
child: const AppView(),
),
);
}
}
class AppView extends StatefulWidget {
const AppView({Key? key}) : super(key: key);
@override
State<AppView> createState() => _AppViewState();
}
class _AppViewState extends State<AppView> {
final _navigatorKey = GlobalKey<NavigatorState>();
NavigatorState get _navigator => _navigatorKey.currentState!;
@override
Widget build(BuildContext context) {
return DynamicColorBuilder(
builder: (ColorScheme? lightColorScheme, ColorScheme? darkColorScheme) {
return OverlaySupport.global(
child: MaterialApp(
title: kLoginAppbarTitle,
theme: AppTheme.lightTheme(lightColorScheme),
darkTheme: AppTheme.darkTheme(darkColorScheme),
themeMode: ThemeMode.system,
debugShowCheckedModeBanner: false,
navigatorKey: _navigatorKey,
builder: (context, child) {
return BlocListener<AuthenticationBloc, AuthenticationState>(
listener: (_, state) {
switch (state.authStatus) {
case AuthenticationStatus.unauthenticated:
_navigator.pushAndRemoveUntil<void>(
LoginPage.route(), (route) => false);
break;
case AuthenticationStatus.authenticated:
_navigator.pushAndRemoveUntil<void>(
HomePage.route(), (route) => false);
break;
default:
break;
}
},
child: child,
);
},
onGenerateRoute: (_) => SplashPage.route(),
),
);
},
);
}
}
splash_page.dart
import 'package:flutter/material.dart';
class SplashPage extends StatelessWidget {
static Route<void> route() {
return MaterialPageRoute<void>(builder: (_) => const SplashPage());
}
const SplashPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: CircularProgressIndicator.adaptive(strokeWidth: 5),
),
);
}
}
正如我在 Flutter Inspector 和一些
print
命令中看到的那样,应用程序正常运行,直到 BlocListener
落在 onGenerateRoute
.
它给了我这个
Launching lib/main.dart on iPhone 14 Pro in debug mode...
Running Xcode build...
Xcode build done. 13,8s
GrMtlCommandBuffer: WARNING: Creating MTLCommandBuffer while in background.
GrMtlCommandBuffer: WARNING: Creating MTLCommandBuffer while in background.
Debug service listening on ws://127.0.0.1:52638/_tIC8TChUyE=/ws
Syncing files to device iPhone 14 Pro...
flutter: dynamic_color: Dynamic color not detected on this device.
我已经删除了 iOS 模拟器上的所有数据,它没有提供任何东西......
在 Android 模拟器上,代码运行得非常好,但在这里,每次在启动画面上它都会失败。
感谢您的帮助!
进入Info.plist。很可能您已经为您的应用程序添加了一个标签以支持多个场景(即多个窗口或屏幕)。它应该是这样的:
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIScene Settings</key>
<dict/>
</dict>
如果你发现它是这样的,请删除允许多个窗口的键。 UISceneConfigurationsUIApplicationSceneManifest
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<true/>
<key>UIScene Settings</key>
<dict/>
</dict>
这就足够了(你可以避免奇怪的黑屏!)