错误:在此 Directioner 小部件上方找不到正确的 Provider<User>
Posted
技术标签:
【中文标题】错误:在此 Directioner 小部件上方找不到正确的 Provider<User>【英文标题】:Error: Could not find the correct Provider<User> above this Directioner Widget 【发布时间】:2021-02-04 21:12:03 【问题描述】:我在运行我的 Flutter 应用程序时遇到了这个错误,我不知道如何修复它。 我怀疑产生此错误的文件是“directioner.dart”,如果没有登录用户,它会显示登录屏幕或主页。 有什么线索吗?谢谢。
这是我的“directioner.dart”文件:
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();
这是我的 main.dart 文件
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(),
);
我的“颤振医生 -v”输出:
[✓] 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!
【问题讨论】:
User
类是 ChangeNotifier
吗?
@AnuragRoy 我不太确定这意味着什么,但如果这对您意味着什么,那么 User 就相当于旧的 Flutter 版本 FirebaseUser 类。感谢您的评论。
【参考方案1】:
您好像忘记在小部件树中“提供”User
对象。
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(),
),
);
错误屏幕通常会给出解决此类问题的提示。
【讨论】:
我认为这非常接近解决我的问题,但不幸的是它仍然无法正常工作:(无论如何谢谢以上是关于错误:在此 Directioner 小部件上方找不到正确的 Provider<User>的主要内容,如果未能解决你的问题,请参考以下文章
ProviderNotFoundException(错误:在此主页小部件上方找不到正确的 Provider<EntryProvider>
我收到错误消息:在此设置小部件上方找不到正确的 Provider<User>(已修复)
错误:在此小部件上方找不到正确的提供程序 <List<Auftrag>>