首页 > 解决方案 > Flutter Firebase - 注册后无法导航到登录屏幕

问题描述

我希望用户在注册后返回登录页面。

我的代码有效,用户注册,但应用程序没有导航到登录页面。

注册页面代码:

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 文件代码

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;
  }
}

标签: firebaseflutterdart

解决方案


试试这种方式:

 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")
       }
            });
          
            }

推荐阅读