I'm writing a code to login and Register with user email and password. And I'm trying to store that data into firestore and firebase storage.
I'm storing the registered person's image in to Firebase Storage. And other detailed information into firestore like name,email etc. Now the image is going to firebaseStorage but the other information that should be stored in firestore, is not going in firestore.
Error:
The registred user is being authenticated
Profile Images are getting uploaded successfully
But the FireStore Storage is Empty, the detailed info of the Registered User isn't approaching here.
Flutter Doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.2.3, on Microsoft Windows [Version 10.0.19043.1200], locale en-PK)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1.0)
[√] Connected device (2 available)
• No issues found!
pub.yaml
name: ecom_app
description: A new Flutter project.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cloud_firestore: ^2.5.0
firebase_auth: ^3.0.2
firebase_core: ^1.5.0
shared_preferences: ^2.0.6
fluttertoast: ^8.0.8
image_picker: ^0.8.3+2
firebase_storage: ^10.0.2
flutter_staggered_grid_view: ^0.4.0
provider: ^6.0.0
path_provider: ^2.0.2
image: ^3.0.2
intl: ^0.17.0
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
# The following section is specific to Flutter.
flutter:
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- images/
# - images/a_dot_ham.jpeg
Config.dart (here I've declared all the requierd values in a class EcommerceApp
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:shared_preferences/shared_preferences.dart';
class EcommerceApp{
static const String appName='E-shop';
static SharedPreferences sharedPreferences=SharedPreferences.getInstance() as SharedPreferences;
static FirebaseAuth auth=FirebaseAuth.instance;
static User user=FirebaseAuth.instance as User;
static FirebaseFirestore firestore=FirebaseFirestore.instance;
static String collectionUser='users';
static String collectionOrder='orders';
static String userCartList='usercart';
static String subCollectionAddress='userAddress';
static final String userName='name';
static final String userEmail ='email';
static final String userPhotoUrl='photoUrl';
static final String userId ='userid';
static final String userAvatarUrl='avatarUrl';
static final String addressId='addressId';
static final String totalAmount='totalAmount';
static final String productId='productId';
static final String paymentDetails='paymentDetails';
static final String orderTime='orderTime';
static final String isSucces='isSucces';
}
Registerpage.dart(registering new a user, no error in code but Informaton isn't storing in firestore)
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:ecom_app/Config/config.dart';
import 'package:ecom_app/DialogBox/errordialog.dart';
import 'package:ecom_app/DialogBox/loadingDialog.dart';
import 'package:ecom_app/Store/StoreHomePage.dart';
import 'package:ecom_app/Widgets/CustomTextField.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class Registration extends StatefulWidget {
const Registration({Key? key}) : super(key: key);
@override
_RegistrationState createState() => _RegistrationState();
}
class _RegistrationState extends State<Registration> {
final TextEditingController _nameController =TextEditingController();
final TextEditingController _emailController =TextEditingController();
final TextEditingController _passwordController =TextEditingController();
final TextEditingController _cpasswordController =TextEditingController();
final GlobalKey<FormState> _formKey=GlobalKey<FormState>();
String userImageUrl='';
File? _imagefile;
@override
Widget build(BuildContext context) {
double _screenwidth=MediaQuery.of(context).size.width,_screenHeight=MediaQuery.of(context).size.height;
return SingleChildScrollView(
child: Container(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
SizedBox(
height: 10.0,
),
Container(
height: _screenHeight*0.25,
width: _screenwidth*0.25,
child:Column(
children: [
Spacer(),
_imagefile !=null ? Image.file(_imagefile!):
CircleAvatar(
radius: _screenwidth * 0.15,
child: Icon(Icons.add_photo_alternate,size: _screenHeight*0.15
,color: Colors.grey),
),
],
)
),
/*InkWell(
onTap: ()=>_PickImage(),
),*/
SizedBox(
height: 8.0,
),
ElevatedButton(
onPressed: ()=>_PickImage(),
child:Icon(Icons.camera)
),
SizedBox(
height: 8.0,
),
Form(
key: _formKey,
child: Column(
children: [
customtextfield(
controller: _nameController,
data: Icons.person,
hinttext: 'Name',
isObsecure:false
),
customtextfield(
controller: _emailController,
data: Icons.email,
hinttext: 'Email',
isObsecure:false,
),
customtextfield(
controller: _passwordController,
data: Icons.lock,
hinttext: 'Password',
isObsecure:true,
),
customtextfield(
controller: _cpasswordController,
data: Icons.security,
hinttext: 'Confirm Password',
isObsecure: true,
),
],
)
),
ElevatedButton(
onPressed: ()=>_uploadandSaveImage(),
style: ElevatedButton.styleFrom(primary: Colors.pink,onPrimary: Colors.deepPurple),
child:Text("Sign up"),
),
SizedBox(
height: 30.0,
),
Container(
height: 4.0,
width: _screenwidth*0.8,
color: Colors.pink,
),
SizedBox(
height: 15.0,
)
],
),
),
);
}
Future<void> _PickImage() async{
final _imagefile =await ImagePicker().pickImage(source: ImageSource.camera);
final _imageselected=File(_imagefile!.path);
setState(() {
this._imagefile=_imageselected;
});
}
Future<void>_uploadandSaveImage() async{
if(_imagefile == null)
{
showDialog(
context: context,
builder: (c)
{
return ErrorDialog(message: 'Please Select an Image');
}
);
}
else
{
_passwordController.text==_cpasswordController.text
? _nameController.text.isNotEmpty &&
_emailController.text.isNotEmpty&&
_passwordController.text.isNotEmpty&&
_cpasswordController.text.isNotEmpty
? uploadToFirebaseStorage()
: displayDialog('Field Can Not Be Empty!...')
: displayDialog('Password Does Not Match');
}
}
displayDialog(String msg) async{
showDialog(
context: context,
builder: (c)
{
return ErrorDialog(message: msg);
}
);
}
uploadToFirebaseStorage()
async {
showDialog(
context: context, builder: (c){
return LoadingAlertDialog(message: 'Authentication Pleas wait...' );
}
);
String imageFileName =DateTime.now().microsecondsSinceEpoch.toString();
Reference storageReference=FirebaseStorage.instance.ref().child(imageFileName);
UploadTask storageUploadTask=storageReference.putFile(_imagefile!);
TaskSnapshot taskSnapshot=await storageUploadTask.whenComplete(() => null);
await taskSnapshot.ref.getDownloadURL().then((urlImage){
userImageUrl=urlImage;
_registerUser();
});
}
FirebaseAuth _auth=FirebaseAuth.instance;
void _registerUser() async{
User? firebaseuser;
await _auth.createUserWithEmailAndPassword(
email: _emailController.text.trim(),
password: _passwordController.text.trim(),
).then((auth)
{
firebaseuser=auth.user!;
}).catchError((error){
Navigator.pop(context);
showDialog(context: context, builder: (c)
{
return ErrorDialog(message: error.message.toString());
});
});
if(firebaseuser != null)
{
savetoFirestore(firebaseuser!).then((value){
Navigator.pop(context);
Route route=MaterialPageRoute(builder: (c)=> StoreHome());
Navigator.pushReplacement(context, route);
});
}
}
savetoFirestore(User fuser) async{
FirebaseFirestore.instance.collection('users').doc(fuser.uid).set(
{
'uid':fuser.uid,
'email': fuser.email,
'name':_nameController.text.trim(),
'url':userImageUrl,
EcommerceApp.userCartList:['garbageValue'],
}
);
await EcommerceApp.sharedPreferences.setString('uid', fuser.uid);
await EcommerceApp.sharedPreferences.setString(EcommerceApp.userEmail, fuser.email!);
await EcommerceApp.sharedPreferences.setString(EcommerceApp.userName, _nameController.text.trim());
await EcommerceApp.sharedPreferences.setString(EcommerceApp.userAvatarUrl, userImageUrl);
await EcommerceApp.sharedPreferences.setStringList(EcommerceApp.userCartList,['garbageValue']);
}
}
UPDATE in Error: