在 Google SignIn 和 Firebase 中验证用户后,Flutter 将电子邮件用户参数传递到另一个页面
Posted
技术标签:
【中文标题】在 Google SignIn 和 Firebase 中验证用户后,Flutter 将电子邮件用户参数传递到另一个页面【英文标题】:Flutter pass email user parameter to another page after validate user in Google SignIn and Firebase 【发布时间】:2018-07-09 07:55:05 【问题描述】:我尝试使用 Firebase / Google SignIn,但是...看看发生了什么?如何使用 Future / async 功能来做到这一点? Scaffold 如何等待 mostraUID() 的返回?你能告诉我实现它的最佳方法吗?
我不知道在 Widget build(BuildContext context)
之前使用 Future / async 将用户电子邮件传递到另一个页面点击FAB后就可以正常使用了。
我需要接收电子邮件用户来编写我的数据库参考 Firebase。
我是 Flutter 的新手。
有没有最好的方法来执行此操作?
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
// Variável de controle de Autenticação Google
final googleSignIn = new GoogleSignIn();
// Variável para gerenciar a Autenticação com o Google Firebase
final auth = FirebaseAuth.instance;
String _loggedUser = "INITIAL VALUE";
String _loggedUserTitle = "NEW TITLE";
void main()
runApp(new MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
class MyHomePage extends StatefulWidget
MyHomePage(Key key, this.title) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
class _MyHomePageState extends State<MyHomePage>
int _counter = 0;
@override
initState()
super.initState();
mostraUID();
Future<Null> mostraUID() async
print("Entrei na mostraUID");
GoogleSignInAccount user = googleSignIn.currentUser;
if(user == null)
user = await googleSignIn.signInSilently();
if(user == null)
user = await googleSignIn.signIn();
// O gerenciamento de TOKEN é implícito
if(await auth.currentUser() == null)
GoogleSignInAuthentication credentials = await googleSignIn.currentUser.authentication;
await auth.signInWithGoogle(
idToken: credentials.idToken,
accessToken: credentials.accessToken,
);
_loggedUser = user.email;
FirebaseUser userFirebase = await FirebaseAuth.instance.currentUser();
print("User no Firebase: " + userFirebase.uid);
print("User no GoogleSignIn: " + user.email);
void _incrementCounter()
setState(()
_counter++;
);
@override
Widget build(BuildContext context)
print("Entrei no Widget build(Build Context context)");
setState(()
_loggedUserTitle = _loggedUser;
);
return new Scaffold(
appBar: new AppBar(
//title: new Text(widget.title),
// *************************************************************************************************** //
// I'll use "message/$_loggedUser" to compose my database reference (Firebase) to show user's messages
title: new Text(_loggedUserTitle),
// *************************************************************************************************** //
),
body: new Center(
child: new Text(
'Button tapped $_counter time$ _counter == 1 ? '' : 's' .',
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
【问题讨论】:
不要以这种方式使用代码 sn-ps。既然你不能运行它,那就无关紧要了。而是使用Code Sample
。
【参考方案1】:
这个问题实际上是一个架构问题。
Flutter 是关于不变性、状态管理和组合的。这是一个相当复杂的话题。所以我会链接一些好文章:
https://hackernoon.com/why-native-app-developers-should-take-a-serious-look-at-flutter-e97361a1c073
https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0
https://www.youtube.com/watch?v=zKXz3pUkw9A
【讨论】:
【参考方案2】:您需要在 Flutter 中处理大量状态操作,并且有很多设计模式可以处理它们。但是,对于您的具体问题;我建议您遵循this 教程,该教程展示了如何使用 BLOC 模式实现 firebase 和 google 登录。
【讨论】:
以上是关于在 Google SignIn 和 Firebase 中验证用户后,Flutter 将电子邮件用户参数传递到另一个页面的主要内容,如果未能解决你的问题,请参考以下文章
Firebase 和 Google Signin 给出重复符号错误
@react-native-google-signin/google-signin/src/GoogleSignin.js 抛出异常 null is not an object(评估'RNGoogle
尝试解析模块“@react-native-community/google-signin”时出错
Firebase团结认证google signIn,下载包删除Unity项目的UI。
通过 gapi.signin2.render 按钮的 Google OAuth 未在反应应用程序中点击回调
java.lang.IllegalArgumentException:服务未注册 Flutter 和 Firebase Google SignIn