为什么我在托管Flutter应用程序时会出现灰色屏幕



我已经检查了托管web应用程序时关于灰屏的所有其他答案,但我还没有找到解决问题的方法。我已经检查了我的代码中的错误,并修复了我的错误。

当我部署我的应用程序时,一切似乎都很好。登录屏幕出现,我可以成功登录并被引导到我的主屏幕。但当我刷新浏览器时,屏幕会变灰,我必须再次部署我的应用程序才能正常工作。

希望有人知道这可能是什么?

调试模式中的一些进一步测试让我出现了这个错误:

以下TypeErrorImpl被抛出构建生成器:意外的null值。导致错误的相关小部件是:材料应用程序材料应用程序:file:///C:/Users//lib/main.dart:50:12

void main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences sharedpreference  = await SharedPreferences.getInstance();
sharedpreference.getString('email');
await Firebase.initializeApp( options: const FirebaseOptions(
apiKey: "AI******YSpsnJ8",
appId: "1:981*******50",
messagingSenderId: "******",
projectId: "tdfsfdf**",
),
);
if (kIsWeb) {
print('Web');
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]
).then((_) => runApp(const MyApp()));
} else {
print('mobil');
// NOT running on the web! You can check for additional platforms here.
runApp(const MyApp());
}
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: FirebaseAuth.instance.currentUser == null
? const UserLoginPage()
: const UserHomePage(),
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: UserLoginPage.id,
routes:{
UserLoginPage.id : (context) => const UserLoginPage(),
AdminServicePage.id : (context) => const AdminServicePage(),
AdminToolsPage.id : (context) => const AdminToolsPage(),
AdminDeviationPage.id : (context) => const AdminDeviationPage(),
AdminUsersPage.id : (context) => const AdminUsersPage(),
UserHomePage.id : (context) => const UserHomePage(),
UserToolListPage.id : (context) => const UserToolListPage(),
WebHomePage.id : (context) => const WebHomePage(),
WebOverviewPage.id : (context) => const WebOverviewPage(),

},
debugShowCheckedModeBanner: false,
);
}
}

如果你不再在调试模式或模拟器上运行应用程序,你会得到灰色屏幕,而不是红色屏幕,这通常是由一些错误调用的。

为了调试,我会添加catcher包来捕获未处理的错误。将包配置为在本地另存为文本文件以供稍后查看,或者您可以在发生时将其发送电子邮件或从其他选项中进行选择。

希望这能有所帮助!

在运行main void之前,您必须初始化Firebase,所以它必须看起来像这样:

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
Firebase.initializeApp(
options: FirebaseOptions(
apiKey: Constants.apiKey,
authDomain: Constants.authDomain,
projectId: Constants.projectId,
storageBucket: Constants.storageBucket,
messagingSenderId: Constants.messagingSenderId,
appId: Constants.appId,
measurementId: Constants.measurementId),
).then((value) {
runApp(const MyApp());
setUrlStrategy(PathUrlStrategy());
});
}

如果没有代码,我们就无法具体判断的问题所在

然而,如果你正在使用一些库/包,请确保它们在网络上得到支持,在Android或Ios 上也得到支持

如果你正在使用firebase,请确保firebase控制台上的项目包括web

最新更新