简体   繁体   中英

sign up/in using firebase flutter

I am trying to log in/signup using a phone number and email, first, the user set the phone number and gets OTP. If this number is linked to the email, then sign in page opens to put the password of the email already registered with this phone number. If the user has no email linked with this phone number, then sign up page will open to put his email and password then the app will open.

here is my code:

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:thrifty/auth/auth_page.dart';
import '../screens/bottom_navigation_bar.dart';
import '../screens/home.dart';
import 'phone_verification.dart';

class WhichScreen extends StatefulWidget {
  const WhichScreen({super.key});

  @override
  State<WhichScreen> createState() => _WhichScreenState();
}

class _WhichScreenState extends State<WhichScreen> {
  @override
  Widget build(BuildContext context) {
    final user = FirebaseAuth.instance.currentUser!;
    return Scaffold(
        body: StreamBuilder<User?>(
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return Center(
            child: CircularProgressIndicator(),
          );
        } else if (snapshot.hasError) {
          return Center(
            child: Text('wrong'),
          );
        } else if (snapshot.hasData) {
          return bottomNavigationBar();
        } else if (user.phoneNumber == '') {
          return PhoneVerification();
        } else if (FirebaseAuth.instance.currentUser!.phoneNumber! == true) {
          return AuthPage();
        } else if (FirebaseAuth.instance.currentUser!.email! == true) {
          return bottomNavigationBar();
        } else {
          return PhoneVerification();
        }
      },
      stream: FirebaseAuth.instance.authStateChanges(),
    ));
  }
}

When I am trying it this error appears:

Null check operator used on a null value

The phone number I entered first already has an email.

Edit this potion of your code.

   } else if (snapshot.hasData) {
          return bottomNavigationBar();
        } else if ((user.phoneNumber??"").isEmpty) {
          return PhoneVerification();
        } else if ((FirebaseAuth.instance.currentUser?.phoneNumber?? "").isNotEmpty) {
          return AuthPage();
        } else if ((FirebaseAuth.instance.currentUser?.email?? "").isNotEmpty) {
          return bottomNavigationBar();
        } else {
          return PhoneVerification();
        }

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