【发布时间】:2021-04-25 17:03:55
【问题描述】:
我是 Flutter 的新手,我在 Youtube 上有一个教程,当应用程序应该向我发送用户 uid 时,我遇到了一个错误,因为代码在新版本的 Firebase 上已过时,即使在互联网上搜索时也是如此我找不到为什么firestore总是给我一个空uid .. 谢谢那些回答我的人我已经被屏蔽了2天了:)
抱歉翻译,我英语不太好,所以我用了google =)
错误来自代码“ Firebase.instance.currentUser.then((”
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_tuto/constants/chargement.dart';
import 'package:flutter_tuto/pageCrud/accueil.dart';
class Inscription extends StatefulWidget {
final Function basculement;
Inscription({ this.basculement });
@override
_InscriptionState createState() => _InscriptionState();
}
class _InscriptionState extends State<Inscription> {
FirebaseAuth _auth = FirebaseAuth.instance;
User courantUser;
//Collection Utilisateur depuis firestore
final CollectionReference collectionUtil = FirebaseFirestore.instance.collection('utilisateurs');
String email = '';
String pseudo = '';
String motDePasse = '';
String confirmeMdp = '';
bool chargement = false;
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
FirebaseAuth.instance.currentUser().then((User util){
setState(() {
this.courantUser = util;
});
});
String _idUtil(){
if(courantUser != null){
return courantUser.uid;
}else{
return "pas d'utilisateurs courant";
}
}
return chargement ? Chargement() : Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/BackgroundImage.jpg"),
fit: BoxFit.cover
)
),
child: Scaffold(
backgroundColor: Colors.transparent,
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.symmetric(vertical: 100.0, horizontal: 30.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Image.asset("assets/logo.png", height: 100.0, width: 100.0),
SizedBox(height: 10.0),
Center(
child: Text("Créer un compte Stix pour accéder à notre appli", style: TextStyle(fontSize: 20.0, color: Colors.blueAccent[100]))
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
labelText: "Adresse Mail",
border: OutlineInputBorder()
),
validator: (val) => val.isEmpty ? 'Entrez une adresse mail' : null,
onChanged: (val) => email = val,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
labelText: "Pseudo",
border: OutlineInputBorder()
),
validator: (val) => val.isEmpty ? 'Entrez votre pseudo' : null,
onChanged: (val) => pseudo = val,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
labelText: "Mot de passe",
border: OutlineInputBorder()
),
validator: (val) => val.length < 6 ? 'Votre mot de passe doit contenir au moins 6 caractères' : null,
onChanged: (val) => motDePasse = val,
obscureText: true,
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
labelText: "Confirmez le mot de passe",
border: OutlineInputBorder()
),
validator: (val) => val != motDePasse ? 'Le mot de passe doit correspondre a la confirmation' : null,
onChanged: (val) => confirmeMdp = val,
obscureText: true,
),
FlatButton(
onPressed: () async {
if (_formKey.currentState.validate()){
setState(() {
chargement = true;
});
UserCredential result = await _auth.createUserWithEmailAndPassword(email: email, password: motDePasse);
await collectionUtil.doc(_idUtil()).set({
'idUtil' : _idUtil(),
'pseudo' : pseudo,
'email' : email
});
if(result == null){
setState(() => chargement = true);
}
}
},
color: Colors.blueAccent,
child: Text("S'inscrire"),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)
),
),
OutlineButton(
onPressed: (){
widget.basculement();
},
borderSide: BorderSide(width: 2.0, color: Colors.blueAccent),
child: Text("Déja Inscrit ?"),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)
),
),
],
)
)
),
),
)
);
}
}
【问题讨论】:
标签: firebase flutter google-cloud-firestore firebase-authentication flutter-dependencies