Flutter:在身份验证后不要重定向

huangapple go评论53阅读模式
英文:

Flutter: do not redirect after authenticating

问题

我在Flutter中做了一个简单的检查,在认证后应该重定向到HomePage()。

我调试了一下,在认证后,authController.isAuth = true,但它没有引用到HomePage()。

class AuthOrHomePage extends StatelessWidget {
  const AuthOrHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    AuthController authController = Provider.of(context);

    return FutureBuilder(
      future: authController.tryAutoLogin(),
      builder: (ctx, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        } else if (snapshot.error != null) {
          return const Center(
            child: Text('Ocorreu um erro!'),
          );
        } else {
          if (authController.isAuth) {
            return const HomePage();
          } else {
            return const InitPage();
          }
        }
      },
    );
  }
}

有什么想法吗?

英文:

I'm doing a simple check in Flutter, where after authenticating, it should redirect to HomePage().

I debugged and after authentication authController.isAuth = true but it doesn't refer to HomePage().

class AuthOrHomePage extends StatelessWidget {
  const AuthOrHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    AuthController authController = Provider.of(context);

    return FutureBuilder(
      future: authController.tryAutoLogin(),
      builder: (ctx, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        } else if (snapshot.error != null) {
          return const Center(
            child: Text('Ocorreu um erro!'),
          );
        } else {
          if (authController.isAuth) {
            return const HomePage();
          } else {
            return const InitPage();
          }
        }
      },
    );
  }
}

Any idea what it could be?

答案1

得分: 0

在Flutter中,如果你想从一个屏幕导航到另一个屏幕,可以使用Navigator

class AuthOrHomePage extends StatelessWidget {
  const AuthOrHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    AuthController authController = Provider.of(context);

    return FutureBuilder(
      future: authController.tryAutoLogin(),
      builder: (ctx, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        } else if (snapshot.error != null) {
          return const Center(
            child: Text('Ocorreu um erro!'),
          );
        } else {
          if (authController.isAuth) {
            Navigator.of(context).push(MaterialPageRoute(builder: (_) => const HomePage()));
          } else {
            Navigator.of(context).push(MaterialPageRoute(builder: (_) => const InitPage()));
          }
          return SizedBox();
        }
      },
    );
  }
}

了解更多:https://api.flutter.dev/flutter/widgets/Navigator-class.html#:~:text=In%20Flutter%20these%20elements%20are,pop.

英文:

In flutter, if you want to navigate from one screen to another you can use Navigator.

class AuthOrHomePage extends StatelessWidget {
  const AuthOrHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    AuthController authController = Provider.of(context);

    return FutureBuilder(
      future: authController.tryAutoLogin(),
      builder: (ctx, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        } else if (snapshot.error != null) {
          return const Center(
            child: Text('Ocorreu um erro!'),
          );
        } else {
          if (authController.isAuth) {
            Navigator.of(context).push(MaterialPageRoute(builder: (_) => const HomePage()));
          } else {
            Navigator.of(context).push(MaterialPageRoute(builder: (_) => const InitPage()));
          }
          return SizedBox();
        }
      },
    );
  }
}

For more : https://api.flutter.dev/flutter/widgets/Navigator-class.html#:~:text=In%20Flutter%20these%20elements%20are,pop.

答案2

得分: 0

尝试这种方式,

    导入 'package:flutter/material.dart';
    导入 'package:provider/provider.dart';

     AuthOrHomePage 扩展 StatelessWidget {
      const AuthOrHomePage({Key? key}) : super(key: key);

      @override
      Widget build(BuildContext context) {
        AuthController authController = Provider.of(context);

        返回 FutureBuilder(
          未来: authController.tryAutoLogin(),
          建造者: (ctx, 快照) {
            如果 (快照.connectionState == ConnectionState.waiting) {
              返回 const 居中(child: CircularProgressIndicator());
            } else if (快照.error != null) {
              返回 const 居中(
                孩子: Text('Ocorreu um erro!'),
              );
            } else {
              如果 (authController.isAuth) {
                // 导航到 HomePage
                WidgetsBinding.instance!.addPostFrameCallback((_) {
                  导航推Replacement(
                    上下文,
                    MaterialPageRoute(builder: (context) => const HomePage()),
                  );
                });
              } else {
                返回 const InitPage();
              }
            }
            返回 const SizedBox.shrink();
          },
        );
      }
    }
英文:

Try this way,

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class AuthOrHomePage extends StatelessWidget {
  const AuthOrHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    AuthController authController = Provider.of(context);

    return FutureBuilder(
      future: authController.tryAutoLogin(),
      builder: (ctx, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        } else if (snapshot.error != null) {
          return const Center(
            child: Text('Ocorreu um erro!'),
          );
        } else {
          if (authController.isAuth) {
            // Navigate to the HomePage
            WidgetsBinding.instance!.addPostFrameCallback((_) {
              Navigator.pushReplacement(
                context,
                MaterialPageRoute(builder: (context) => const HomePage()),
              );
            });
          } else {
            return const InitPage();
          }
        }
        return const SizedBox.shrink();
      },
    );
  }
}

huangapple
  • 本文由 发表于 2023年3月31日 02:14:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定