我一直在努力解决这个错误一段时间,不知道如何解决它。抛出的错误是:
The following FirebaseException was thrown building KeyedSubtree-[GlobalKey#a1dff]:
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
我已经初始化了firebase应用程序,所以我真的很困惑为什么会发生这种情况。我尝试过的一些事情:
检查我在PubSpec.yaml中的依赖项
重新运行我的代码并停止应用程序并重新启动
向ChatGPT求助(无用)
下面是我的代码:import 'package:flutter/material.dart';
import 'package:workout_app/Screens/Components/Sign_Up_Screens/screen1.dart';
import 'package:workout_app/Screens/Components/login.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:shared_preferences/shared_preferences.dart';
class HomePage extends StatefulWidget {
@override
const HomePage({Key? key}) : super(key: key);
State<StatefulWidget> createState() => Main();
}
class Main extends State<HomePage> {
final FirebaseAuth _auth = FirebaseAuth.instance;
String? uid;
String? name;
String? userEmail;
String? imageUrl;
@override
void initState() {
super.initState();
start();
}
void start() async {
await Firebase.initializeApp();
SharedPreferences prefs = await SharedPreferences.getInstance();
bool? authSignedIn = prefs.getBool('auth');
print(authSignedIn);
final User? user = _auth.currentUser;
if (authSignedIn == true) {
if (user != null) {
uid = user.uid;
name = user.displayName;
userEmail = user.email;
imageUrl = user.photoURL;
prefs.setString('name', name!);
prefs.setString('id', uid!);
}
}
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold (
backgroundColor: Color.fromRGBO(57, 62, 70, 1),
body: Container (
height: size.height,
width: double.infinity,
child: Stack(
alignment: Alignment.center,
children: <Widget> [
Positioned(
top: size.height * .2,
child: Image.asset(
"assets/logos/logo.png",
width: size.width * .8
)
),
Positioned(
top: size.height * .65,
child: Column(
children: const [
Text(
'Welcome Back!',
style: TextStyle(color: Colors.white, fontSize: 20)
)
]
),
),
Positioned(
top: size.height * .72,
child: SizedBox(
width: size.width * .9,
height: size.height * .08,
child: ElevatedButton(
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(99),
)
),
backgroundColor: MaterialStateProperty.all<Color>(Colors.green),
),
child: const Text('Sign Up',
style: TextStyle(fontSize: 25),
),
onPressed: () {
Navigator.of(context).push(_createRouteToSignUp());
},
),
),
),
Positioned(
top: size.height * .81,
child: SizedBox(
width: size.width * .9,
height: size.height * .08,
child: ElevatedButton(
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(99),
)
),
backgroundColor: MaterialStateProperty.all<Color>(Color.fromRGBO(43, 46, 51, 1)),
),
child: const Text('Log In',
style: TextStyle(fontSize: 20),
),
onPressed: () {
Navigator.of(context).push(_createRouteToLogin());
},
),
),
),
Positioned(
top: size.height * .96,
left: size.width * .71,
child: Column(
children: const [
Text('Version 1.0.0.0',
style: TextStyle(color: Colors.white)
),
]
)
),
]
)
));
}
}
Route _createRouteToLogin() {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => login_page(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(0.0, 1.0);
const end = Offset.zero;
const curve = Curves.ease;
var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
return SlideTransition(
position: animation.drive(tween),
child: child,
);
},
);
}
Route _createRouteToSignUp() {
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => screen1(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(0.0, 1.0);
const end = Offset.zero;
const curve = Curves.ease;
var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
return SlideTransition(
position: animation.drive(tween),
child: child,
);
},
);
}
非常感谢!!
问题是,你需要在void main中初始化你的firebase,或者你需要使用FutureBuilder等待,直到你的firebase初始化完成。
,
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(Main());
}
或
,
FutureBuilder(
// Initialize Flutter Firebase
future: Firebase.initializeApp(),
builder: (context, snapshot) {
// Check for errors
if (snapshot.hasError) {
return SomethingWentWrong();
}
// Once complete, show your application
if (snapshot.connectionState == ConnectionState.done) {
return Main();
}
// Otherwise, show something whilst waiting for initialization to complete
return Loading();
},
);