CodeingTestPrac

3월 8일 ,flutter async await

sung.hyun.1204 2022. 3. 8. 11:27

firebase 로그인을 위해 await 를 적어준다  . 왜 ???

 

sync -동기 

async -비동기

await  - 내려가는거 기다려!  먼저 하고 가자.

 

1.

await 를 하지 않을시 다음 페이지로 이동 후 구글 로그인 창이 나온다.

 

2. iscomplieteregis() 를 사용해 firebase 가 아닌 개인 서버에서 유저의 유무를확인하는데 

여기도 await 가 없으면 코드가 끝나고 나서 iscomplieteregis() 가 작동한다.

 

개인 서버의 아이디가 있어도 .

await 가 없다,  checkvalide = false -> print("no account ") ->  iscomplieteregis() ->check valide true , 

await 가 있다,   iscomplieteregis() ->check valide true ->checkvalide = true -> print("there is account ") , 

main .dart

async{
    await FirebaseService().signInwithGoogle();

    CurrentUser cucheck = CurrentUser();
    await cucheck.iscomplieteregis();

      if (cucheck.checkvalide == true)
      {
        print("there is account");
        Navigator.pushNamedAndRemoveUntil(
            context, HomePage.id, (route) => false);
      } else // go to addtional sign
      {
        print("no account");
        Navigator.pushNamedAndRemoveUntil(
            context, Regisinput.id, (route) => false);
      }

    },

 

currentservice.dart

import 'package:http/http.dart' as http;
import 'package:firebase_auth/firebase_auth.dart' as fbs;

class CurrentUser {
    final _auth =fbs.FirebaseAuth.instance;

    bool checkvalide = false;

    iscomplieteregis()
          async {
            final user = await _auth.currentUser!;
            final String Authorization = user.uid as String;
            final response = await http.get(
                Uri.parse("~/api/user/"),
                headers: {
                  "Authorization" : Authorization,
                  "Content-Type": "application/json",
                }
            );

            print("this is  response code ");
            print(response.statusCode);
            if (response.statusCode == 200)
            {
              checkvalide = true;
              print("thisi is check val:");
              print(checkvalide);
              return ;

            }
            else if(response.statusCode == 500)
            {
              print("no account in spring server");
              checkvalide = false;
              return ;
            }
            else {
              print('servererror');
              checkvalide = false;
              return ;
            }
          }



}