【发布时间】:2021-08-27 10:42:57
【问题描述】:
我目前正在尝试从 FirebaseAuth 获取当前用户状态,并在用户未注销时在主屏幕之间切换,如果用户已注销,则在启动屏幕之间切换。用户注册和登录都成功并在firestore数据库中注册。但是每次我从手机关闭应用程序并重新打开时,MediaQuery.of(context) 宽度都有一个空值并显示错误。
但底部导航栏仍在显示。当我重新构建应用程序并再次登录时它可以工作,它会将我带到主屏幕。仅当我关闭并重新打开应用程序时才会发生。
import 'package:country_code_picker/country_localizations.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'routes.dart';
import 'package:screens/home/home_screen.dart';
import 'package:screens/splash/splash_screen.dart';
import 'constants.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
supportedLocales: [Locale('en', 'US')],
localizationsDelegates: [CountryLocalizations.delegate],
title: 'App',
theme: theme(),
routes: routes,
home: LandingPage(),
);
}
}
class LandingPage extends StatelessWidget {
const LandingPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final Future<FirebaseApp> _init = Firebase.initializeApp();
return FutureBuilder(
future: _init,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Scaffold(
body: Center(
child: Text("Error: ${snapshot.error}"),
),
);
}
if (snapshot.connectionState == ConnectionState.done) {
return StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, streamSnapshot) {
if (streamSnapshot.hasError) {
return Scaffold(
body: Center(
child: Text("Error: ${streamSnapshot.error}"),
),
);
}
if (streamSnapshot.connectionState == ConnectionState.active) {
print(streamSnapshot.data.toString());
if (streamSnapshot.data == null) {
return SplashScreen();
} else {
return HomeScreen();
}
}
return Scaffold(
body: Center(
child: CircularProgressIndicator(
backgroundColor: Colors.white,
color: kPrimaryColor,
strokeWidth: 3,
),
),
);
},
);
}
return Scaffold(
body: Center(
child: CircularProgressIndicator(
backgroundColor: Colors.white,
color: kPrimaryColor,
strokeWidth: 3,
),
),
);
},
);
}
}
也许我认为这是未来构建器或流构建器的问题?你们中的任何人都有解决此问题的方法或其他实现方法。
Launching lib/main.dart on STK L21 in debug mode...
✓ Built build/app/outputs/flutter-apk/app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:33019/yIPGCqY2gwY=/ws
I/flutter ( 4249): locale.languageCode: en
D/AwareBitmapCacher( 4249): handleInit switch not opened pid=4249
I/flutter ( 4249): User(displayName: , email: rohanbhautoo@gmail.com, emailVerified: false, isAnonymous: false, metadata: UserMetadata(creationTime: 2021-06-10 08:20:44.583, lastSignInTime: 2021-06-10 15:50:23.573), phoneNumber: , photoURL: null, providerData, [UserInfo(displayName: , email: rohanbhautoo@gmail.com, phoneNumber: , photoURL: null, providerId: password, uid: rohanbhautoo@gmail.com)], refreshToken: , tenantId: null, uid: yviaYOJ5ziayFQhvhvemB3MjkPr1)
════════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building Body(dirty):
Null check operator used on a null value
The relevant error-causing widget was
Body
When the exception was thrown, this was the stack
#0 getProportionateScreenWidth
#1 Body.build
#2 StatelessElement.build
#3 ComponentElement.performRebuild
#4 Element.rebuild
...
════════════════════════════════════════════════════════════════════════════════
Size_config.dart
import 'package:flutter/material.dart';
class SizeConfig {
static MediaQueryData? _mediaQueryData;
static double? screenWidth;
static double? screenHeight;
static double? defaultSize;
static Orientation? orientation;
void init(BuildContext context) {
_mediaQueryData = MediaQuery.of(context);
screenWidth = _mediaQueryData!.size.width;
screenHeight = _mediaQueryData!.size.height;
orientation = _mediaQueryData!.orientation;
}
}
// Proportionate height per screen size
double getProportionateScreenHeight(double inputHeight) {
double? screenHeight = SizeConfig.screenHeight;
return (inputHeight / 812.0) * screenHeight!;
}
// Proportionate width per screen size
double getProportionateScreenWidth(double inputWidth) {
double? screenWidth = SizeConfig.screenWidth;
return (inputWidth / 375.0) * screenWidth!;
}
#0 的堆栈跟踪行是 return (inputWidth / 375.0) * screenWidth!;它显示我在 screenWidth 上使用空检查!。
【问题讨论】:
-
更多细节会有所帮助。你也有来自控制台的堆栈跟踪吗?
-
@nilsmagnus 是的没问题,我已经添加了堆栈跟踪。
标签: firebase flutter dart firebase-authentication