Home > Software design >  Flutter ProviderNotFoundException Issue
Flutter ProviderNotFoundException Issue

Time:11-11

I want use Firebase Auth in Flutter project. And I am use provider. Everything is okey but I am facing one issue with provider.

My IconButtonWidget:

class SocialIconButton extends StatelessWidget {
  final String socialIcon;
  const SocialIconButton({Key? key, required this.socialIcon})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: context.dynamicWidth(20)),
      child: IconButton(
          onPressed: (() {
            final provider =
                Provider.of<GoogleSignInProvider>(context, listen: false);
            provider.login();
          }),
          icon: Image.asset(socialIcon)),
    );
  }
}

When I press button I am facing this issue:

ProviderNotFoundException (Error: Could not find the correct Provider<GoogleSignInProvider> above this SocialIconButton Widget.

CodePudding user response:

Wrap your App with MultiProvider and provide instance like

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => GoogleSignInProvider()),
      ],
      child: const MyApp(),
    ),
  );
}

More about provider. Also you can check riverpod2

CodePudding user response:

Bedfore use provider you need to initialized it in your main.dart file like this

void main() {
  runApp(
    /// Providers are above [MyApp] instead of inside it, so that tests
    /// can use [MyApp] while mocking the providers
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => GoogleSignInProvider()),
      ],
      child: const MyApp(),
    ),
  );
}

after that rebuild the app

  • Related