添加 Firebase.initializeApp() 后,我的应用卡在加载屏幕上,没有 Firebase.initializeApp(),它会显示我的登录屏幕,但我无法在不初始化 firebase_core 的情况下使用 pubspec.yaml 文件中的任何 firebase 插件。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: themeData(),
home:Login()
);
}
Run Code Online (Sandbox Code Playgroud)
试过这个,它加载错误屏幕错误:未来返回空
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
// Initialize FlutterFire
future: Firebase.initializeApp(),
builder: (context, snapshot) {
// Check for errors
if (snapshot.hasError) {
print(snapshot.error.toString);
return Error();
}
// …Run Code Online (Sandbox Code Playgroud) 我收到错误未处理的异常:未找到“动态”。当我调用 authController 时,您需要调用“Get.put(dynamic())”或“Get.lazyPut(()=>dynamic())”,下面是我的代码。
if (Get.find<AuthController>().isLoggedIn()) {
//statements
}
Run Code Online (Sandbox Code Playgroud)
我的初始化函数
Future init() async {
// Core
final sharedPreferences = await SharedPreferences.getInstance();
Get.lazyPut(() => sharedPreferences);
Get.lazyPut(() => ApiClient(
appBaseUrl: AppConstants.BASE_URL,
));
// Repository
Get.lazyPut(() => AuthRepo(apiClient:Get.find(), sharedPreferences: Get.find()));
// controller
Get.lazyPut(() => AuthController(authRepo: Get.find()));
Run Code Online (Sandbox Code Playgroud)
}
主要方法
void main() async{
await di.init();
runApp(
child: MyApp(),
),
);
}
Run Code Online (Sandbox Code Playgroud)