我试图添加启动画面以将启动画面添加到我的应用程序,但是它卡在启动画面italf中,并且无法移动到下一个画面
我在这里添加了代码:

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    getUserInfo();
  }

  Future getUserInfo() async {
    await getUser();
    setState(() {});
    print(uid);
    navigateUser();
  }
  navigateUser()
  {
    if(uid!=null && authSignedIn != false)
      {
        Timer(Duration(seconds: 2),
            ()=>Navigator.pushReplacementNamed(context, "/toprofilepage")

    );
      }
    else{
      Timer(Duration(seconds: 2),
          ()=>Navigator.pushReplacementNamed(context, "/tologinpage")
      );
    }
  }



  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Login',
      initial route:'/',
      routes: {
        '/toprofilepage':(context)=>FirstScreen(),
        '/tologinpage':(context)=>LoginPage(),
      },
      home: Scaffold(
        body: Center(
          child: Text("Saraswat",style: TextStyle(fontSize: 40,fontWeight: FontWeight.bold,fontStyle: FontStyle.italic),),
        ),
      )
    );
  }
}

我也在控制台中收到以下消息:

登录代码:-
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();

bool authSignedIn;
String uid;
String name;
String imageUrl;
Future getUser() async {
  // Initialize Firebase
  await Firebase.initializeApp();

  SharedPreferences prefs = await SharedPreferences.getInstance();
  bool authSignedIn = prefs.getBool('auth') ?? false;

  final User user = _auth.currentUser;

  if (authSignedIn == true) {
    if (user != null) {
      uid = user.uid;
      name = user.displayName;

      imageUrl = user.photoURL;
    }
  }
}

Future<String> signInWithGoogle() async {
  // Initialize Firebase
  await Firebase.initializeApp();

  final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
  final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication;

  final AuthCredential credential = GoogleAuthProvider.credential(
    accessToken: googleSignInAuthentication.accessToken,
    idToken: googleSignInAuthentication.idToken,
  );

  final UserCredential userCredential = await _auth.signInWithCredential(credential);
  final User user = userCredential.user;

  if (user != null) {
    // Checking if email and name is null
    assert(user.uid != null);

    assert(user.displayName != null);
    assert(user.photoURL != null);

    uid = user.uid;
    name = user.displayName;

    imageUrl = user.photoURL;

    assert(!user.isAnonymous);
    assert(await user.getIdToken() != null);

    final User currentUser = _auth.currentUser;
    assert(user.uid == currentUser.uid);

    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool('auth', true);

    return 'Google sign in successful, User UID: ${user.uid}';
  }

  return null;
}

void signOutGoogle() async {
  await googleSignIn.signOut();
  await _auth.signOut();

  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setBool('auth', false);

  uid = null;
  name = null;

  imageUrl = null;

  print("User signed out of Google account");
}

我尝试了很多事情,但是没有得到任何解决方案。请把导航器功能放在其他任何地方,或者是否有其他错误请求帮助!

最佳答案

尝试仅在initialRoute中进行流动条件
由于Firebase已更新,我们检查用户是否登录的方式。它不是异步任务,因此您可以直接在MyApp类中使用。

initialRoute: FirebaseAuth.instance.currentUser != null
          ? HomeScreen.route_name
          : AuthScreen.route_name
或者您可以使用侦听器进行身份验证更改
FirebaseAuth.instance.onAuthStateChanged.listen((firebaseUser) {
    // do whatever you want based on the firebaseUser state
  });
因此,更改身份验证后,它将重定向到您想要的页面,就像这样
home: StreamBuilder(
        stream: FirebaseAuth.instance.onAuthStateChanged,
        builder: (streamContext, userSnapshot) {
          if (userSnapshot.connectionState == ConnectionState.waiting)
            return SplashScreen();
          if (userSnapshot.hasData) {
            return HomeScreen();
          }
          return AuthScreen();
        },
      ),

10-07 18:10