authentication - 如果用户已经登录,则让他们直接进入仪表板页面(Google Auth with Flutter)
问题描述
如果用户已经登录,我如何跳过登录页面并直接进入主屏幕?
我从以下网址看到了教程:https ://youtu.be/RIiQSd_C1hs
我想获得简单的解决方案,例如自动检查您是否登录然后转到仪表板页面
这是我用于这个项目的代码
主要飞镖:
import 'package:login/home.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
主页.dart
import 'package:google_sign_in/google_sign_in.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool _isLoggedIn = false;
GoogleSignInAccount _userObj;
GoogleSignIn _googleSignIn = GoogleSignIn();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Codesundar")),
body: Container(
child: _isLoggedIn
? Column(
children: [
Image.network(_userObj.photoUrl),
Text(_userObj.displayName),
Text(_userObj.email),
TextButton(
onPressed: () {
_googleSignIn.signOut().then((value) {
setState(() {
_isLoggedIn = false;
});
}).catchError((e) {});
},
child: Text("Logout"))
],
)
: Center(
child: ElevatedButton(
child: Text("Login with Google"),
onPressed: () {
_googleSignIn.signIn().then((userData) {
setState(() {
_isLoggedIn = true;
_userObj = userData;
});
}).catchError((e) {
print(e);
});
},
),
),
),
);
}
}
保护你
解决方案
推荐阅读
- docker - 如何为非 docker Jenkins master 创建 docker slave 构建
- php - 提交表单按钮后重定向到 2 个不同的页面
- python - 预期 2 个值得到一个函数
- cassandra-3.0 - 如何在 Embedded-Cassandra 中获取特定版本的 Cassandra
- sql - 为“stat”多次指定了“30”列
- java - 为什么即使启用了 Wi-Fi 呼叫,方法 telephonyManager.getVoiceNetworkType() 也会返回 Network_LTE 的值?
- c# - AWS Lambda 函数在 .Net 核心中调用自身
- typescript - 打字稿什么是更好的方法类或接口
- asp.net - 如何从 ajax 选项卡容器中获取客户端 ID
- mysql - mysql中只有一条记录的“limit 10”和“limit 100”有什么区别吗?