在扑波中使用提供商功能后,如何导航到另一页?

发布于 2025-01-21 12:15:34 字数 2201 浏览 4 评论 0原文

我已经在登录提供商中创建了一个函数,以验证下面的应用程序的OTP。

Future<bool> verifyOtp(String otp) async {
    final _loginData = await SharedPreferences.getInstance();

    _isLoading = true;
    notifyListeners();
    _status = await AuthApi.verifyOtp(otp);
    _isLoading = false;
    _name = _loginData.getString('name');
    notifyListeners();
    return _status;
  }

现在,每当我尝试在下面的代码上使用它时,

final bool status = await Provider.of<LoginProvider>(context, listen: false).verifyOtp(verificationCode);

// ignore: avoid_print
print("STATUS ==== " + status.toString());
if (status) {
  Navigator.of(context).pushReplacementNamed('/discover');
} else {
  ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Incorrect OTP!!!")));
}

它给我一个例外,如下 -

Exception has occurred.
FlutterError (This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.)

任何人,请指导我,从提供商导航的实际方法是什么。我在提供商中非常新。非常感谢:)

-----完整的提供商代码在下面-----

class LoginProvider with ChangeNotifier {
  bool _status = false;
  bool _isLoading = false;
  bool _isOtpScreen = false;
  String? _name;

  bool get isLoading => _isLoading;
  bool get isOtpScreen => _isOtpScreen;
  String? get name => _name;

  void sendOtp(String phone) async {
    _isLoading = true;
    notifyListeners();
    _status = await AuthApi.sendOtp(phone);
    _isLoading = false;
    _isOtpScreen = true;
    notifyListeners();
  }

  Future<bool> verifyOtp(String otp) async {
    final _loginData = await SharedPreferences.getInstance();

    _isLoading = true;
    notifyListeners();
    _status = await AuthApi.verifyOtp(otp);
    _isLoading = false;
    _name = _loginData.getString('name');
    notifyListeners();
    return _status;
  }
}

I have created a function in my Login provider to verify OTP for my app like below.

Future<bool> verifyOtp(String otp) async {
    final _loginData = await SharedPreferences.getInstance();

    _isLoading = true;
    notifyListeners();
    _status = await AuthApi.verifyOtp(otp);
    _isLoading = false;
    _name = _loginData.getString('name');
    notifyListeners();
    return _status;
  }

Now whenever I am trying to use this on my code like below,

final bool status = await Provider.of<LoginProvider>(context, listen: false).verifyOtp(verificationCode);

// ignore: avoid_print
print("STATUS ==== " + status.toString());
if (status) {
  Navigator.of(context).pushReplacementNamed('/discover');
} else {
  ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Incorrect OTP!!!")));
}

It's giving me an exception like below -

Exception has occurred.
FlutterError (This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.)

Anyone please guide me, what is the actual way to navigate from a provider. I am very new in Provider. Thank you so much :)

----- Full Provider Code is Below -----

class LoginProvider with ChangeNotifier {
  bool _status = false;
  bool _isLoading = false;
  bool _isOtpScreen = false;
  String? _name;

  bool get isLoading => _isLoading;
  bool get isOtpScreen => _isOtpScreen;
  String? get name => _name;

  void sendOtp(String phone) async {
    _isLoading = true;
    notifyListeners();
    _status = await AuthApi.sendOtp(phone);
    _isLoading = false;
    _isOtpScreen = true;
    notifyListeners();
  }

  Future<bool> verifyOtp(String otp) async {
    final _loginData = await SharedPreferences.getInstance();

    _isLoading = true;
    notifyListeners();
    _status = await AuthApi.verifyOtp(otp);
    _isLoading = false;
    _name = _loginData.getString('name');
    notifyListeners();
    return _status;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆头 2025-01-28 12:15:34

使用全球键可以从任何地方访问

创建键

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
void main() async {
 WidgetsFlutterBinding.ensureInitialized();
   runApp(MyApp());
}

将其传递给您的应用程序:

new MaterialApp(
  title: 'MyApp',
  onGenerateRoute: generateRoute,
  navigatorKey: navigatorKey,
);

在您的路线中使用:

    print("STATUS ==== " + status.toString());
    if (status) {
       Navigator.of(navigatorKey.currentContext).pushReplacementNamed('/discover');
    } else {
      ScaffoldMessenger.of(navigatorKey.currentContext).showSnackBar(const SnackBar(content:
      Text("Incorrect OTP!!!")));
    }

Use a GlobalKey you can access from anywhere to navigate

Create the key

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
void main() async {
 WidgetsFlutterBinding.ensureInitialized();
   runApp(MyApp());
}

Pass it to your App:

new MaterialApp(
  title: 'MyApp',
  onGenerateRoute: generateRoute,
  navigatorKey: navigatorKey,
);

Use in your route:

    print("STATUS ==== " + status.toString());
    if (status) {
       Navigator.of(navigatorKey.currentContext).pushReplacementNamed('/discover');
    } else {
      ScaffoldMessenger.of(navigatorKey.currentContext).showSnackBar(const SnackBar(content:
      Text("Incorrect OTP!!!")));
    }
许一世地老天荒 2025-01-28 12:15:34
final bool status = await Provider.of<LoginProvider>(context, listen: false).verifyOtp(verificationCode);

// ignore: avoid_print
print("STATUS ==== " + status.toString());
if (status) {
  Navigator.of(context).pushReplacementNamed('/discover');
} else {
  ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Incorrect OTP!!!")));
}

将此代码更改为下一个代码,我认为它将起作用
如果它是无状态的小部件,则必须在构建函数中添加此代码

final provider = Provider.of<LoginProvider>(context);

final status = await provider.verifyOtp(verificationCode);

// ignore: avoid_print
print("STATUS ==== " + status.toString());
if (status) {
  Navigator.of(context).pushReplacementNamed('/discover');
} else {
  ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Incorrect OTP!!!")));
}

但是,

final bool status = await Provider.of<LoginProvider>(context, listen: false).verifyOtp(verificationCode);

// ignore: avoid_print
print("STATUS ==== " + status.toString());
if (status) {
  Navigator.of(context).pushReplacementNamed('/discover');
} else {
  ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Incorrect OTP!!!")));
}

change this code to the next one and i think it will work
but you must add this code in build function if it's stateless widget

final provider = Provider.of<LoginProvider>(context);

final status = await provider.verifyOtp(verificationCode);

// ignore: avoid_print
print("STATUS ==== " + status.toString());
if (status) {
  Navigator.of(context).pushReplacementNamed('/discover');
} else {
  ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Incorrect OTP!!!")));
}

this will work fine with you i hope it help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文