嗨,我正在处理 firebase 身份验证,用户/名称密码在经过身份验证时应该显示一个新活动和一个新屏幕,但它没有这样做。我似乎无法理解问题所在
代码如下: Main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<AuthenticationService>(
create: (_) => AuthenticationService(FirebaseAuth.instance),
),
StreamProvider(
create: (context) =>
context.read<AuthenticationService>().authStateChanges,
)
],
child: MaterialApp(
home: AuthenticationWrapper(),
),
);
}
}
class AuthenticationWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final firebaseUser = context.watch<User>();
if (firebaseUser != null) {
return HomePage();
}
return PatientList();
}
}
主要是main.dart文件有认证码
HomePage.dart
class HomePage extends StatelessWidget {
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
child: Column(
children: <Widget>[
Container(
height: 400,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/loginHeader.png'),
fit: BoxFit.fill)),
child: Stack(
children: <Widget>[],
),
),
Padding(
padding: EdgeInsets.all(30.0),
child: Column(
children: <Widget>[
FadeAnimation(
1.8,
Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: Color.fromRGBO(143, 148, 251, .2),
blurRadius: 20.0,
offset: Offset(0, 10))
]),
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.grey[100]))),
child: TextField(
controller: emailController,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Email or Phone number",
hintStyle:
TextStyle(color: Colors.grey[400])),
),
),
Container(
padding: EdgeInsets.all(8.0),
child: TextField(
controller: passwordController,
obscureText: true,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Password",
hintStyle:
TextStyle(color: Colors.grey[400])),
),
)
],
),
)),
SizedBox(
height: 30,
),
RaisedButton(
padding: EdgeInsets.only(
left: 100, right: 100, top: 20, bottom: 20),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(28.0),
side: BorderSide(color: Colors.red)),
onPressed: () {
context.read<AuthenticationService>().signIn(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
},
color: Color.fromRGBO(214, 0, 27, 1),
textColor: Colors.white,
child: Text("Login".toUpperCase(),
style: TextStyle(fontSize: 14)),
),
Padding(
padding: EdgeInsets.only(top: 15),
),
ClipOval(
child: RaisedButton(
onPressed: () {
gotoForgotPassword(BuildContext context) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ForgotPassword()),
);
}
gotoForgotPassword(context);
},
child: Text("Forgot Password"),
textColor: Colors.white,
color: Color.fromRGBO(214, 0, 27, 1),
),
),
],
),
)
],
),
),
),
);
}
}
这有登录屏幕,主要是应用的用户界面,它有按钮导航到应用的其他活动。
class AuthenticationService {
final FirebaseAuth _firebaseAuth;
AuthenticationService(this._firebaseAuth);
Stream<User> get authStateChanges => _firebaseAuth.idTokenChanges();
Future<String> signIn({String email, String password}) async {
try {
await _firebaseAuth.signInWithEmailAndPassword(
email: email, password: password);
return "Signed in";
} on FirebaseAuthException catch (e) {
return e.message;
}
}
Future<String> signUp({String email, String password}) async {
try {
await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
return "Signed up";
} on FirebaseAuthException catch (e) {
return e.message;
}
}
}
知道我做错了什么吗?我想是一些导航问题。
当用户输入正确的用户名和密码时,它应该导航到另一个屏幕,但它没有。我还检查了 firebase 控制台,它正在建立连接并告诉 1 位用户在 2 月 17 日登录
答案 0 :(得分:1)
看起来您应该在 AuthenticationWrapper 类中使用 StreamBuilder 小部件。
class AuthenticationWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<User>(
stream: context.read<AuthenticationService>().authStateChanges,
builder: (_, AsyncSnapshot<User> snapshot) {
final User firebaseUser = snapshot.data;
if (firebaseUser != null) {
return HomePage();
}
return PatientList();
}
);
}
}
答案 1 :(得分:1)
将代码放入 main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<AuthenticationService>(
create: (_) => AuthenticationService(FirebaseAuth.instance),
),
StreamProvider(
create: (context) =>
context.read<AuthenticationService>().authStateChanges,
)
],
child: MaterialApp(
// home: AuthenticationWrapper(),
home: StreamBuilder(
stream:FirebaseAuth.instance.authStateChanges(),
builder:(BuildContext context, AsyncSnapshot<User>snapshot){
if(snapshot.hasData){
return HomePage(),
}
return PatientList(),
}
)
),
);
}
}