简体   繁体   中英

Flutter Firebase - Unable to Navigate to Login Screen after Register

I want user to return to login page after registering.

My code works, user get registered, but app doesnt navigate to Login page.

Register page codes:

 import 'package:flutter/material.dart'; import 'package:iddmib_rev/screens/haber_screen.dart'; import 'package:iddmib_rev/screens/login_screen.dart'; import 'package:iddmib_rev/services/auth.dart'; class RegisterScreen extends StatefulWidget { @override _RegisterScreenState createState() => _RegisterScreenState(); } class _RegisterScreenState extends State<RegisterScreen> { final TextEditingController _nameController = TextEditingController(); final TextEditingController _emailController = TextEditingController(); final TextEditingController _passwordController = TextEditingController(); final TextEditingController _passwordAgainController = TextEditingController(); AuthService _authService = AuthService(); @override Widget build(BuildContext context) { return Scaffold( body: SingleChildScrollView( child: Container( decoration: BoxDecoration( color: Colors.grey[100], ), height: MediaQuery.of(context).size.height, child: Column( children: <Widget>[ Image( height: MediaQuery.of(context).size.height / 3.5, image: AssetImage('assets/images/logo.png'), ), Padding( padding: EdgeInsets.symmetric(horizontal: 60.0, vertical: 10.0), child: TextField( controller: _nameController, decoration: InputDecoration( contentPadding: EdgeInsets.symmetric(vertical: 25.0), fillColor: Colors.white, filled: true, hintText: 'İsim', prefixIcon: Icon( Icons.person_outlined, size: 30.0, ), ), ), ), Padding( padding: EdgeInsets.symmetric(horizontal: 60.0, vertical: 10.0), child: TextField( controller: _emailController, decoration: InputDecoration( contentPadding: EdgeInsets.symmetric(vertical: 25.0), fillColor: Colors.white, filled: true, hintText: 'E-posta', prefixIcon: Icon( Icons.email_outlined, size: 30.0, ), ), ), ), Padding( padding: EdgeInsets.symmetric(horizontal: 60.0, vertical: 10.0), child: TextField( controller: _passwordController, obscureText: true, decoration: InputDecoration( contentPadding: EdgeInsets.symmetric(vertical: 25.0), fillColor: Colors.white, filled: true, hintText: 'Şifre', prefixIcon: Icon( Icons.password_outlined, size: 30.0, ), ), ), ), Padding( padding: EdgeInsets.symmetric(horizontal: 60.0, vertical: 10.0), child: TextField( controller: _passwordAgainController, obscureText: true, decoration: InputDecoration( contentPadding: EdgeInsets.symmetric(vertical: 25.0), fillColor: Colors.white, filled: true, hintText: 'Şifre Tekrar', prefixIcon: Icon( Icons.password_outlined, size: 30.0, ), ), ), ), SizedBox(height: 10.0), InkWell( onTap: () { _authService.createPerson(_nameController.text, _emailController.text, _passwordController.text).then((value) { return Navigator.push(context, MaterialPageRoute(builder: (context) => LoginScreen())); }); }, child: Container( margin: EdgeInsets.symmetric(horizontal: 60.0), alignment: Alignment.center, height: 60.0, decoration: BoxDecoration( color: Theme.of(context).primaryColor, borderRadius: BorderRadius.circular(5.0), ), child: Text( 'Kayıt Ol', style: TextStyle( color: Colors.white, fontSize: 25.0, ), ), ), ), SizedBox(height: 10.0), ], ), )), ); } }

Auth.dart file codes

 import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; class AuthService { final FirebaseAuth _auth = FirebaseAuth.instance; final FirebaseFirestore _firestore = FirebaseFirestore.instance; //giriş yap fonksiyonu Future<User?> signIn(String email, String password) async { var user = await _auth.signInWithEmailAndPassword( email: email, password: password); return user.user; } //çıkış yap fonksiyonu signOut() async { return await _auth.signOut(); } //kayıt ol fonksiyonu Future<User?> createPerson(String name, String email, String password) async { var user = await _auth.createUserWithEmailAndPassword( email: email, password: password); await _firestore.collection("Person").doc(user.user..uid):set({'userName', name: 'email'; email}). return user;user; } }

Try this way:

 onTap:(){

   _authService
                .createPerson(_nameController.text, _emailController.text,
                    _passwordController.text)
                .then((value) {

    if(value !=null){
              return Navigator.push(context,
                  MaterialPageRoute(builder: (context) =>LoginScreen()));
     }
 else{
    debugPrint("user is not authenticated")
       }
            });
          
            }

Try this:

            InkWell(
              onTap: () async {
                await _authService
                    .createPerson(_nameController.text, _emailController.text,
                        _passwordController.text);
                return Navigator.push(context,
                      MaterialPageRoute(builder: (context) => LoginScreen()));
              },
              child: Container(
                margin: EdgeInsets.symmetric(horizontal: 60.0),
                alignment: Alignment.center,
                height: 60.0,
                decoration: BoxDecoration(
                  color: Theme.of(context).primaryColor,
                  borderRadius: BorderRadius.circular(5.0),
                ),
                child: Text(
                  'Kayıt Ol',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 25.0,
                  ),
                ),
              ),

I solved this issue by changing this; Firebase Console>Project>Firestore Database>Rules

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

To this;

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM