简体   繁体   English

Flutter 登录Provider AuthService

[英]Flutter login Provider AuthService

My application was working fine but after I update the whole package and flutter with the android studio it shows an error in the login section when I login/signup below is the error:我的应用程序运行良好,但是在我使用 android 工作室更新整个 package 和 flutter 后,当我登录/注册时,它在登录部分显示错误如下:

======== Exception caught by gesture ===================================================
The following ProviderNotFoundException was thrown while handling a gesture:
Error: Could not find the correct Provider<AuthService> above this SignIn Widget

This happens because you used a BuildContext that does not include the provider of your choice.发生这种情况是因为您使用的BuildContext不包括您选择的提供者。 There are a few common scenarios:有几种常见的场景:

  • You added a new provider in your main.dart and performed a hot-reload.您在main.dart中添加了一个新提供程序并执行了热重载。 To fix, perform a hot-restart.要修复,请执行热重启。

  • The provider you are trying to read it in a different route.您尝试以不同方式读取它的提供程序。

    Providers are "scoped".提供者是“范围的”。 So if you insert of provider inside a route, then other routes will not be able to access that provider.因此,如果您在路由中插入提供程序,那么其他路由将无法访问该提供程序。

  • You used a BuildContext that is an ancestor of the provider you are trying to read.您使用的BuildContext是您尝试读取的提供程序的祖先。

    Make sure that SignIn is under your MultiProvider/Provider.确保 SignIn 在您的 MultiProvider/Provider 下。 This usually happens when you are creating a provider and trying to read it immediately.这通常发生在您创建提供程序并尝试立即读取它时。

    For example, instead of:例如,而不是:

     Widget build(BuildContext context) { return Provider<Example>( create: (_) => Example(), // Will throw a ProviderNotFoundError, because `context` is associated // to the widget that is the parent of `Provider<Example>` child: Text(context.watch<Example>()), ), }

    consider using builder like so:考虑像这样使用builder

     Widget build(BuildContext context) { return Provider<Example>( create: (_) => Example(), // we use `builder` to obtain a new `BuildContext` that has access to the provider builder: (context) { // No longer throws return Text(context.watch<Example>()), } ), }

    When the exception was thrown, this was the stack:抛出异常时,这是堆栈:

     #0 Provider._inheritedElementOf (package:provider/src/provider.dart:329:7) #1 Provider.of (package:provider/src/provider.dart:281:30) #2 ReadContext.read (package:provider/src/provider.dart:607:21) #3 _SignInState._toggleSignInButton (package:wallet_ui_app/pages/Login/pages/login_page.dart:391:13) #4 _SignInState.build.<anonymous closure> (package:wallet_ui_app/pages/Login/pages/login_page.dart:376:21) #5 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:989:21) #6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:193:24) #7 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:608:11) #8 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5) #9 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:267:7) #10 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:157:27) #11 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:444:20) #12 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:420:22) #13 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:278:11) #14 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7) #15 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5) #16 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7) #17 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7) #21 _invoke1 (dart:ui/hooks.dart:185:10) #22 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:293:7) #23 _dispatchPointerDataPacket (dart:ui/hooks.dart:98:31) (elided 3 frames from dart:async) Handler: "onTap" Recognizer: TapGestureRecognizer#0cb65 debugOwner: GestureDetector state: ready won arena finalPosition: Offset(216.1, 537.3) finalLocalPosition: Offset(98.3, 28.9) button: 1 sent tap down

I will post now below the code that I am getting an error from:我现在将在我收到错误的代码下方发布:

  void _onSignInButtonPress() {
    _pageController.animateToPage(0,
        duration: const Duration(milliseconds: 500), curve: Curves.decelerate);
  }

 void _toggleSignInButton() {
    print('_toggleSignInButton clicked');
    final String email = loginEmailController.text.trim();
    final String password = loginPasswordController.text.trim();
    context.read<AuthService>().login(
      email,
      password,
    ).whenComplete(() => IfAdminLoggedIn());
  }

Authservices.dart: Authservices.dart:

 class AuthService {
  final FirebaseAuth _auth;

  AuthService(this._auth);

  Stream<User> get authStateChanges => _auth.idTokenChanges();

  Future<String> login(String email, String password) async {

await _auth.signInWithEmailAndPassword(email: email, password: password);
}

is there any solution for this?有什么解决办法吗?

Use riverpod package its easier than provider and now with update packages provider deprecated and got too much bugs about (Provider not found)使用riverpod package 它比提供者更容易,现在更新包提供者已弃用并且有太多关于(未找到提供者)的错误

The simplest solution for all providers is to include them at the root of your application, above MaterialApp.对于所有提供者来说,最简单的解决方案是将它们包含在您的应用程序的根目录中,在 MaterialApp 之上。

And for the purpose of finding and debugging issues related to these providers I recommend using dart dev tools on a browser and checking out the widget hierarchy and see if the widget where you're trying to access the provider at falls under the hierarchy or not.为了查找和调试与这些提供程序相关的问题,我建议在浏览器上使用 dart 开发工具并检查小部件层次结构并查看您尝试访问提供程序的小部件是否属于层次结构。

I had a similar issue earlier and I solved this using dart dev tools (I was easily able to spot that the provider I added was falling out of hierarchy and was not accessible, ie there was a sibling relation between widgets, which I did not expect, resulting the provider to be inaccessible).我之前遇到过类似的问题,我使用 dart 开发工具解决了这个问题(我很容易发现我添加的提供程序脱离了层次结构并且无法访问,即小部件之间存在兄弟关系,这是我没想到的,导致提供者无法访问)。

Hope this also works for you.希望这也对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM