gpt4 book ai didi

firebase - 错误:在此Director小部件上方找不到正确的Provider

转载 作者:行者123 更新时间:2023-12-03 02:59:04 25 4
gpt4 key购买 nike

运行Flutter应用程序时出现此错误,我不知道如何解决它。
我怀疑产生此错误的文件是“directioner.dart”,如果没有登录用户,它将显示“登录”屏幕或“主页”。
有什么线索吗?谢谢。
enter image description here
这是我的“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(),
);
}
}
我的'flutter doctor -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对象。

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(),
),
);
}
}
错误屏幕通常会提供解决此类问题的提示。

关于firebase - 错误:在此Director小部件上方找不到正确的Provider <User>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64472297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com