Flutter Firebase Storage - [核心/无应用] 未创建 Firebase 应用"[默认]"错误



我正在尝试上传文件到Firebase Storage。但是我在完整安装部分得到一个错误。

代码:

final Storage storage = Storage();
...
FloatingActionButton.large(
child: Icon(Icons.folder, color: Colors.white, size: 45,),
backgroundColor: Color.fromARGB(255, 36, 36, 36),
onPressed: () async {
final results = await FilePicker.platform.pickFiles(
type: FileType.image,
allowMultiple: false,
allowedExtensions: ['jpg', 'png', "jpeg"],
);
if (results == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Not selected image."),
backgroundColor: Color.fromARGB(255, 36, 36, 36),
),
);
return null;
}
final path = results.files.single.path!;
final fileName = results.files.single.name;
storage.uploadFile(path, fileName);
},
),

Storageclass:

import 'dart:io';
import 'package:firebase_storage/firebase_storage.dart';
class Storage {
final FirebaseStorage _storage = FirebaseStorage.instance;
Future<void> uploadFile(String filePath, String fileName) async {
File file = File(filePath);
try {
await _storage.ref("$fileName").putFile(file);
} on Exception catch (e) {
print(e);
}
}
}

在上传完整图像时,我得到以下错误:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
#0      MethodChannelFirebase.app
package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:193
#1      Firebase.app
package:firebase_core/src/firebase.dart:53
#2      FirebaseStorage.instance
package:firebase_storage/src/firebase_storage.dart:51
#3      new Storage
package:bolataktar_resimyukleme/firebase/storage_services.dart:6
#4      storage
package:bolataktar_resimyukleme/screens/resimyukleme_main.dart:11

main.dart:

import 'package:bolataktar_resimyukleme/screens/resimyukleme_main.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_storage/firebase_storage.dart';
import "package:fluent_ui/fluent_ui.dart";
import 'package:firedart/firedart.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: BolatAktarApp(),
);
}
}

我面临同样的问题,这里是我在项目

中所做的更改在AndroidManifest.xml

在应用程序标签中添加以下服务

</application>
...
<service android:name="com.google.firebase.components.ComponentDiscoveryService" >
<meta-data
android:name="com.google.firebase.components:com.google.firebase.analytics.connector.internal.AnalyticsConnectorRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
<meta-data
android:name="com.google.firebase.components:com.google.firebase.firestore.FirestoreRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar"/>
<meta-data
android:name="com.google.firebase.components:com.google.firebase.iid.Registrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
<meta-data
android:name="com.google.firebase.components:com.google.firebase.auth.FirebaseAuthRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
<meta-data
android:name="com.google.firebase.components:com.google.firebase.database.DatabaseRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar"/>
<meta-data
android:name="com.google.firebase.components:com.google.firebase.storage.StorageRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
</service>
</application>

不要忘记在manifest

中添加internet权限
<uses-permission android:name="android.permission.INTERNET"/>

在app/build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: "com.google.gms.google-services" //This is new line
.....
compileSdkVersion 31 

Project build.gradle

buildscript {
ext.kotlin_version = '1.6.10' //you can use updated version here.
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.13'//New Added with version 4.3.13 older one was not able to find by android studio.
}
}
allprojects {
repositories {
google()
jcenter()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'com.google.gms.google-services'//New Line

flutter clean

关闭Android studio并重新启动(不知道为什么,但关闭studio有效),然后。

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

对于测试目的,您可以像这样更改firebase存储规则

rules_version = '3';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write : if true
// allow read, write: if request.auth != null; //Use this when your testing Done only authentic user should be able to upload
}
}
}

相关内容

  • 没有找到相关文章

最新更新