如何在颤动中使用提供者功能后导航到另一个页面?

How to navigate to another page after using a provider function in flutter?

我在我的登录提供程序中创建了一个函数来验证我的应用程序的 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.)

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

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

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;
  }
}

使用您可以从任何地方访问的 GlobalKey 进行导航

创建密钥

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!!!")));
    }
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!!!")));
}

这对你来说很好,希望对你有帮助