Reputation: 463
I'll preface this by saying that I'm pretty new to Flutter and I'm building a Messenger / Chat app. I'm using Firebase to register the user, using Email/Password and I'm doing that successfully.
This is my SignIn.dart
import 'package:chat_app/helper/helperfunctions.dart';
import 'package:chat_app/services/auth.dart';
import 'package:chat_app/services/database.dart';
import 'package:chat_app/widgets/widgets.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'chatRoomsScreen.dart';
class SignIn extends StatefulWidget {
late final Function toggle;
SignIn(this.toggle);
@override
_SignInState createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
final formKey = GlobalKey<FormState>();
TextEditingController emailTextEditingController = new TextEditingController();
TextEditingController passwordTextEditingController = new TextEditingController();
AuthMethods authMethods = new AuthMethods();
DatabaseMethods databaseMethods = new DatabaseMethods();
bool isLoading = false;
late QuerySnapshot<Map<String, dynamic>>? snapShotUserInfo;
signIn() async {
if (formKey.currentState!.validate()) {
HelperFunctions.saveUserEmailSharedPreference(emailTextEditingController.text);
setState(() {
isLoading = true;
});
databaseMethods.getUserByUserEmail(emailTextEditingController.text).then((val) {
snapShotUserInfo = val;
HelperFunctions
.saveUserNameSharedPreference(snapShotUserInfo?.docs[0].data()["name"]);
print("${snapShotUserInfo?.docs[0].data()["name"]} is my name");
});
authMethods.signInWithEmailAndPassword(emailTextEditingController.text, passwordTextEditingController.text)
.then((val) {
if (val != null) {
HelperFunctions.saveUserLoggedInSharedPreference(true);
Navigator.pushReplacement(context,
MaterialPageRoute(
builder: (context) => ChatRoom())
);
}
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: appBarMain(context),
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.fromLTRB(0, 80, 0, 0),
alignment: Alignment.bottomCenter,
child: Container(
padding: EdgeInsets.symmetric(
horizontal: 24.0
),
child: Form(
key: formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
validator: (val) {
return RegExp(
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
.hasMatch(val!)
? null
: "Please Enter Correct Email";
},
controller: emailTextEditingController,
style: simpleTextStyle(),
decoration: textFieldInputDecoration("email")
),
TextFormField(
validator: (val) {
return val!.length > 6 ? null : "Provide a longer password!";
},
controller: passwordTextEditingController,
style: simpleTextStyle(),
obscureText: true,
decoration: textFieldInputDecoration("password")
),
SizedBox(height: 12),
Container(
alignment: Alignment.centerRight,
padding: EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
child: Text("Forgot Password?",
style: simpleTextStyle(),),
),
SizedBox(height: 8),
GestureDetector(
onTap: () {
signIn();
},
child: Container(
alignment: Alignment.center,
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.symmetric(vertical: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
gradient: LinearGradient(
colors: [
Colors.amber,
Colors.amber
]
)
),
child: Text(
"Sign In",
style: TextStyle(
color: Colors.black,
fontSize: 16
)
),
),
),
SizedBox(height: 16),
Container(
alignment: Alignment.center,
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.symmetric(vertical: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.white,
),
child: Text(
"Sign In with Google",
style: TextStyle(
color: Colors.black,
fontSize: 16
)
),
),
SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Don't have account? ", style: mediumTextStyle()),
GestureDetector(
onTap: () {
widget.toggle();
},
child: Container(
padding: EdgeInsets.symmetric(vertical: 8),
child: Text("Register now!",style: TextStyle(
color: Colors.amber,
fontSize: 16,
decoration: TextDecoration.underline,
),),
),
),
],
),
SizedBox(height: 50),
],
),
),
),
),
),
);
}
}
HelperFunctions.dart (using SharedPreferences package):
import 'package:shared_preferences/shared_preferences.dart';
class HelperFunctions {
static String sharedPreferenceUserLoggedInKey = "ISLOGGEDIN";
static String sharedPreferenceUserNameKey = "USERNAMEKEY";
static String sharedPreferenceUserEmailKey = "USEREMAILKEY";
static Future<bool> saveUserLoggedInSharedPreference(bool isUserLoggedIn) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setBool(sharedPreferenceUserLoggedInKey, isUserLoggedIn);
}
static Future<bool> saveUserNameSharedPreference(String userName) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setString(sharedPreferenceUserNameKey, userName);
}
/// ---
static Future<bool> saveUserEmailSharedPreference(String userEmail) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setString(sharedPreferenceUserEmailKey, userEmail);
}
static Future<bool?> getUserLoggedInSharedPreference() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool(sharedPreferenceUserLoggedInKey);
}
static Future<String?> getUserNameSharedPreference() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(sharedPreferenceUserNameKey);
}
static Future<String?> getUserEmailSharedPreference() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(sharedPreferenceUserEmailKey);
}
}
DatebaseMethods.dart:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
class DatabaseMethods {
getUserByUsername(String username) async {
return await FirebaseFirestore.instance.collection("users")
.where("name", isEqualTo: username).get();
}
getUserByUserEmail(String userEmail) async {
return await FirebaseFirestore.instance.collection("users")
.where("name", isEqualTo: userEmail).get();
}
uploadUserInfo(userMap) {
FirebaseFirestore.instance.collection("users").add(userMap);
}
createChatRoom(String chatRoomId, chatRoomMap) {
FirebaseFirestore.instance.collection("ChatRoom")
.doc(chatRoomId).set(chatRoomMap).catchError((e) {
print(e.toString());
});
}
}
AuthMethods.dart:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:chat_app/modal/appUser.dart';
import 'package:firebase_core/firebase_core.dart';
class AuthMethods {
final FirebaseAuth _auth = FirebaseAuth.instance;
appUser? _userFromFirebaseUser(User user) {
return user != null ? appUser(userId: user.uid) : null;
}
Future signInWithEmailAndPassword(String email, String password) async {
try {
UserCredential result = await _auth.signInWithEmailAndPassword(
email: email, password: password);
User? firebaseUser = result.user;
return _userFromFirebaseUser(firebaseUser!);
} catch (e) {
print(e);
}
}
Future signUpWithEmailAndPassword(String email, String password) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword
(email: email, password: password);
User? firebaseUser = result.user;
return _userFromFirebaseUser(firebaseUser!);
} catch(e) {
print(e.toString());
}
}
Future resetPass(String email) async {
try {
return await _auth.sendPasswordResetEmail(email: email);
} catch(e) {
print(e.toString());
}
}
Future signOut() async {
try {
return await _auth.signOut();
} catch(e) {
print(e.toString());
}
}
}
Everything else is working, hell - I'm even saving the email of the logged in users in the Firebase User Database, but I'm unable to fetch the username whenever I sign-in, what am I doing wrong? You can see that I've tried printing the username quite a few times (as I need it to proceed to Chat Rooms), what am I doing wrong here?
Upvotes: 2
Views: 397
Reputation: 828
in getUserByUsername
the return type is dynamic
You need to specify the return type as Future<String>
and await that value when you invoke the method.
Upvotes: 1