I can’t pass step 5 of “Get to know Firebase for Flutter” tutorial

I am trying to learn flutter. I started to “Get to know Firebase for Flutter” tutorial.

I am getting provider error the end of step 5 of this tutorial (Add RSVP functionality).
https://firebase.google.com/codelabs/firebase-get-to-know-flutter?authuser=0#4

When the following code is added to the hope_page.dart file, it gives below error.

Thanks.

// Add from here
          Consumer<ApplicationState>(
            builder: (context, appState, _) => AuthFunc(
                loggedIn: appState.loggedIn,
                signOut: () {
                  FirebaseAuth.instance.signOut();
                }),
          ),
// to here

hope_page.dart

import 'package:firebase_auth/firebase_auth.dart' // new
   hide EmailAuthProvider, PhoneAuthProvider;    // new
import 'package:flutter/material.dart';           // new
import 'package:provider/provider.dart';          // new

import 'app_state.dart';                          // new
import 'src/authentication.dart';                 // new
import 'src/widgets.dart';

class HomePage extends StatelessWidget {
 const HomePage({super.key});

 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       title: const Text('Firebase Meetup'),
     ),
     body: ListView(
       children: <Widget>[
         Image.asset('assets/codelab.png'),
         const SizedBox(height: 8),
         const IconAndDetail(Icons.calendar_today, 'October 30'),
         const IconAndDetail(Icons.location_city, 'San Francisco'),
         // Add from here
         Consumer<ApplicationState>(
           builder: (context, appState, _) => AuthFunc(
               loggedIn: appState.loggedIn,
               signOut: () {
                 FirebaseAuth.instance.signOut();
               }),
         ),
         // to here
         const Divider(
           height: 8,
           thickness: 1,
           indent: 8,
           endIndent: 8,
           color: Colors.grey,
         ),
         const Header("What we'll be doing"),
         const Paragraph(
           'Join us for a day full of Firebase Workshops and Pizza!',
         ),
       ],
     ),
   );
 }
}

error code:

This app is linked to the debug service: ws://127.0.0.1:59701/r_M_M0NpAZc=/ws
Debug service listening on ws://127.0.0.1:59701/r_M_M0NpAZc=/ws
Connecting to VM Service at ws://127.0.0.1:59701/r_M_M0NpAZc=/ws
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following ProviderNotFoundException was thrown building Consumer<ApplicationState>(dirty):
Error: Could not find the correct Provider<ApplicationState> above this Consumer<ApplicationState>
Widget

This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:

- You added a new provider in your `main.dart` and performed a hot-reload.
  To fix, perform a hot-restart.

- The provider you are trying to read is 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.

  Make sure that Consumer<ApplicationState> is under your MultiProvider/Provider<ApplicationState>.
  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>().toString()),
    );
  }

  consider using `builder` like so:

  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, child) {
        // No longer throws
        return Text(context.watch<Example>().toString());
      }
    );
  }


If none of these solutions work, consider asking for help on StackOverflow:
https://stackoverflow.com/questions/tagged/flutter

The relevant error-causing widget was:
  Consumer<ApplicationState>
  Consumer:file:///D:/flutter_projeler/flutter-codelabs/firebase-get-to-know-flutter/step_02/lib/home_page.dart:26:11

When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:3       throw_
packages/provider/src/provider.dart 343:7                                         _inheritedElementOf
packages/provider/src/provider.dart 293:30                                        of
packages/provider/src/consumer.dart 181:16                                        buildWithChild
packages/nested/nested.dart 259:41                                                build
packages/flutter/src/widgets/framework.dart 5541:22                               build
packages/nested/nested.dart 279:18                                                build
packages/flutter/src/widgets/framework.dart 5471:15                               performRebuild
packages/flutter/src/widgets/framework.dart 5187:7                                rebuild
packages/flutter/src/widgets/framework.dart 2895:18                               buildScope
packages/flutter/src/widgets/binding.dart 984:9                                   drawFrame
packages/flutter/src/rendering/binding.dart 457:5                                 [_handlePersistentFrameCallback]
packages/flutter/src/scheduler/binding.dart 1325:7                                [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1255:9                                handleDrawFrame
packages/flutter/src/scheduler/binding.dart 1113:5                                [_handleDrawFrame]
lib/_engine/engine/platform_dispatcher.dart 1274:5                                invoke
lib/_engine/engine/platform_dispatcher.dart 248:5                                 invokeOnDrawFrame
lib/_engine/engine/initialization.dart 186:36                                     <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 574:37  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 579:39  dcall

════════════════════════════════════════════════════════════════════════════════════════════════════
Application finished.

Exited (-1).```

Leave a Comment