Flutter Firebase Storage not woriking: no default bucket
Asked Answered
O

5

7

I am trying to upload a pdf-file to Firebase-Storage using this function:

  static Future<String> savePdf({
    required Uint8List assetAsUint8List,
    required String fileName,
    required DocumentType documentType,
  }) async {
    String path =
        '${BackendService().keys.studs}/${AuthenticationService().currentUser?.uid}/${documentType.name}/$fileName';

    await FirebaseStorage.instanceFor().ref(path).putData(
          assetAsUint8List,
        );
    return FirebaseStorage.instance.ref(path).getDownloadURL();
  }

but this fails with this error:

Unhandled Exception: [firebase_storage/no-bucket] No default storage bucket could be found. Ensure you have correctly followed the Getting Started guide.

I configure my app like this inside my main:

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: kIsWeb || Platform.isAndroid
        ? FirebaseOptions(
            apiKey: "my-api-key",
            appId: "my-app-id",
            messagingSenderId: "my-messaing-sender-id",
            projectId: "appflug",
          )
        : null,
  );
  runApp(
    const App(),
  );
}

It is actually working on iOS! But NOT on Android or Web...

I followed the documentation but it is simply not working... I coulnd't find anything helpful on this!

What am I missing here?

Let me know if you need any more info!

Opposition answered 18/1, 2022 at 22:30 Comment(0)
O
21

The solution was rather simple: I needed to add the storageBucket in my FirebaseOptions so my main looks like this:

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: kIsWeb || Platform.isAndroid
        ? FirebaseOptions(
            apiKey: "my-app-key",
            appId: "my-app-id",
            messagingSenderId: "my-messaging-sender-id",
            projectId: "my-project-id",
            storageBucket: "myapp.appspot.com",
          )
        : null,
  );
  runApp(
    const App(),
  );
}
Opposition answered 19/1, 2022 at 10:39 Comment(0)
L
2

in your code replace:

FirebaseStorage.instance

instead:

FirebaseStorage.instanceFor(
bucket: "gs://your-bucket-name.com"
)

good luck with your app.

Lydialydian answered 16/4, 2024 at 18:20 Comment(1)
Thank you some much! The other solution did not work for me, but this fixed my problem.Pommel
M
1

If you working on android then The solution was simple: I needed to add the storageBucket from google-services.json in my FirebaseOptions so my main looks like this:

 WidgetsFlutterBinding.ensureInitialized();
 await Firebase.initializeApp(
    options: FirebaseOptions(
        apiKey: 'AIzaSyAz2kNBgLq_vJw2WjzSkdt2HwVP1k814TI',
        appId: '1:288911819468:android:ba34dab02e6268dc683869',
        messagingSenderId: '288911812458',
        projectId: 'fir-series-e6357',
        storageBucket: 'fir-series-e6357.appspot.com'
    )
  );
  runApp(const MyApp());
Massa answered 8/1, 2024 at 20:26 Comment(0)
B
0

In your main.dart file follow this steps.It worked for me I hope this works for you too. Posted on 14 june 2024

    Future <void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(
        options: const FirebaseOptions(
            apiKey:'go-at-firebase-console/project setting/[Web/app api key]',
            appId: 'go-at-firebase-console/project setting/appID',
            messagingSenderId: 'go-at-firebase-console/project setting/messagingSenderID',
            projectId: 'project/settings/projectID',
            storageBucket: 'project/settings/storage/storageID'
        )
      );
//Rest of your code 
Bleak answered 13/7, 2024 at 0:20 Comment(0)
V
0

Just as @ali alsadadi mentioned before you should use:

 // Get a non-default Storage bucket
 //sometime even if you only have one bucker(the default one) this will still happen
 final storage = FirebaseStorage.instanceFor(bucket: "gs://my-custom-bucket");

you can read more about this on the official docs: https://firebase.google.com/docs/storage/flutter/start#use_multiple_storage_buckets

Something to note is that this issue will not happen on IOS builds just on android builds.

My recommendation is to try this solution before adding the storage option on your Firebase.Initialize().

you should also check your package version as well as your gradle dependencies versions:

// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:33.1.2')
// TODO: Add the dependencies for Firebase products you want to use
// When using the BoM, don't specify versions in Firebase dependencies
// https://firebase.google.com/docs/android/setup#available-libraries

// https://mvnrepository.com/artifact/com.google.firebase/firebase-firestore
implementation 'com.google.firebase:firebase-firestore'

remember that this should be inside of your dependencies block in your build.gradle app level, you can expand more on this on the official docs: https://firebase.google.com/docs/android/learn-more#bom

feel free to edit or comment to expand on the solution for this issue :)

Vilma answered 27/8, 2024 at 18:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.