如果用户未明确退出应用,我正在尝试自动登录。我正在使用Firebase进行身份验证,并使用pub的提供程序包来监视auth状态的更改。
我有一个服务dart文件(最初来自TheNetNinja教程),该文件公开一个流,并用流提供程序包装main.dart
文件。据我了解,在provider 4.0.1
中,无法再使用.value and supply the value
,如下所示:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider<User>.value(
value: AuthService().user,
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Brewzz',
home: Wrapper(),
),
);
}
}
但要更改为此类内容,请使用create instead of value
,如下所示:
class FoodMart extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider(
create: (_) => AuthService().user,
child: MaterialApp(
title: 'Flutter Demo',
onGenerateRoute: Router.generateRoute,
home: Landing(),
),
);
}
}
我已关闭并重新打开该应用程序,但仍未重定向到主页。我做错了吗?
这是AuthService.dart
类的实现:
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:food_mart/models/users.dart';
import 'package:food_mart/services/database.dart';
class AuthService {
FirebaseAuth _mAuth = FirebaseAuth.instance;
Stream<User> get user {
return _mAuth.onAuthStateChanged.map(_mapUserFromFirebase);
}
User _mapUserFromFirebase(FirebaseUser firebaseUser) {
return firebaseUser != null ? User(userId: firebaseUser.uid) : null;
}
Future createUser(String email, String pwd, String role) async{
try {
AuthResult result = await _mAuth.createUserWithEmailAndPassword(email: email, password: pwd);
FirebaseUser firebaseUser = result.user;
await DatabaseService(userId: firebaseUser.uid).setUserData(email: email, role: role);
return _mapUserFromFirebase(firebaseUser);
} catch(e){
print(e.toString());
return e;
}
}
Future loginUser(String email, String pwd) async{
try {
AuthResult authResult = await _mAuth.signInWithEmailAndPassword(email: email, password: pwd);
FirebaseUser firebaseUser = authResult.user;
return _mapUserFromFirebase(firebaseUser);
} catch(e){
print(e.toString());
return null;
}
}
Future signOut() async {
try {
return await _mAuth.signOut();
} catch(e){
print(e.toString());
return null;
}
}
}
为简洁起见,User
模型已被省略。有什么建议吗?谢谢
答案 0 :(得分:0)
我遇到了类似的问题,但是上面的代码起作用了。
class Wrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
if(user == null){
return Landing();
}else{
return Index();
}
}
}
就像您一样,它并没有改变我的页面。事实证明代码一直有效,只有我从另一个名为LoginWithEmail
的页面登录用户,该页面在Landing
类的按钮事件中被压入堆栈。登录后,我才在Navigator.pop()
上做了一个LoginWithEmail
,意识到Wrapper
类已更改为Index
。
//code call inside login button onpressed from LoginWithEmail class
AuthService _authService = AuthService();
_authService.loginUser(email, password).then((value) {
User user = value;
if(user!=null){
Navigator.pop();
}
}