Error: Could not find the correct Provider<User> above this Directioner Widget

Viewed 97

I'm getting this error when running my Flutter app and I have no clue about how to fix it. I suspect that the file that produces this error is 'directioner.dart' which shows Login screen or Home instead if there's no loged in user. Any clue? Thanks.

enter image description here

This is my 'directioner.dart' file:

import 'package:empirica_qaqc/pages/home_page.dart';
import 'package:empirica_qaqc/pages/login_page.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class Directioner extends StatelessWidget {
  const Directioner({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<User>(context);
    return user == null ? LoginPage() : HomePage();
  }
}

and this is my main.dart file

import 'package:empirica_qaqc/pages/directioner.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primaryColor: Color(0xff1e8735),
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Directioner(),
    );
  }
}

My 'flutter doctor -v' output:

[✓] Flutter (Channel stable, 1.22.1, on Mac OS X 10.15.6 19G2021, locale es-CL)
    • Flutter version 1.22.1 at /Users/ben/flutter
    • Framework revision f30b7f4db9 (13 days ago), 2020-10-08 10:06:30 -0700
    • Engine revision 75bef9f6c8
    • Dart version 2.10.1

 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/ben/Library/Android/sdk
    • Platform android-30, build-tools 30.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.6)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.6, Build version 11E708
    • CocoaPods version 1.9.1

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 50.0.1
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.50.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.15.1

[✓] Connected device (1 available)
    • sdk gphone x86 arm (mobile) • emulator-5554 • android-x86 • Android 11 (API 30) (emulator)

• No issues found!
1 Answers

Seems like you forgot to "provide" the User object in your widget tree.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primaryColor: Color(0xff1e8735),
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      // expose an object to be available down the widget tree
      home: Provider( 
        create: (_) => User(),
        child: Directioner(),
      ),
    );
  }
}

The error screen typically gives hints to solve this type of issues.

Related