Stream Builder 不起作用:我希望它在用户登录时移至下一页。
我收到了用于登录但未由 Stream Builder 执行导航的电子邮件的令牌。
这是 Main.dart:
class _ThisAppState extends State<ThisApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: _handleWindowDisplay(),
),
);
}
}
Widget _handleWindowDisplay() {
StreamBuilder(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (context, asyncSnapshot) {
if (asyncSnapshot.hasError) {
return new Text("Error!");
} else if (asyncSnapshot.data == null) {
return new LoginPage();
} else {
if (asyncSnapshot.data.state) {
return new LoginPage();
} else
return MainScreen();
}
},);
}
class _ThisAppState extends State<ThisApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: _handleWindowDisplay(),
),
);
}
}
Widget _handleWindowDisplay() {
StreamBuilder(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (context, asyncSnapshot) {
if (asyncSnapshot.hasError) {
return new Text("Error!");
} else if (asyncSnapshot.data == null) {
return new LoginPage();
} else {
if (asyncSnapshot.data.state) {
return new LoginPage();
} else
return MainScreen();
}
},);
}
**Login Screen**
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
String _email,_Password;
FutureOr Function(AuthResult value) get onValue => null;
@override
Widget build(BuildContext context) {
return Container(
child:
Form(child:
Column(
children: <Widget>[
TextFormField(decoration: InputDecoration(labelText: "Enter Email"),
onChanged: (value){
this.setState((){_email=value;});
},
),
SizedBox(height: 10,),
TextFormField(decoration: InputDecoration(labelText: "Enter Password"),
onChanged:(value) {
this.setState(() {
_Password = value;
});
},
),
SizedBox(height: 10,),
RaisedButton(child: Text("Sign In"),onPressed: ()=>{
FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _Password)
.then((onValue){
}).catchError((error){
debugPrint("The error is "+error);
})
})
],
),
),
);
}
}