I'm writing a mobile application in Flutter and have encountered an issue relating to Firebase Storage. Part of the application needs to take an picture with the device's camera and upload it to a Firebase Storage bucket.
The upload function was working until I added some authentication functionality among other small changes.
Here is the issue:
It appears that the Firebase storage instance is uninitialized. I have seen some examples showing that Firebase may be initialized with a function that takes the api key, bucket, and such as arguments. The google documentation never refers to this function, however, and it instead recommends to copy GoogleService-Info.plist and google-services.json into the project. I've done this, and it works. The authentication api and the Firestore api, both of which I am also using in the app, both work properly. It is just Firebase storage that is problematic.
In my main function, for instance, I added the following code to show the problem.
Future<void> main() async{
// setup camera
WidgetsFlutterBinding.ensureInitialized();
final cameras = await availableCameras();
// look here!!
print('firestore: ${Firestore.instance.app}');
print('storage: ${FirebaseStorage.instance.app}');
runApp(MyApp(cameras: cameras));
}
And upon running the app (on a physical device), this is the console output:
Restarted application in 1,097ms.
flutter: firestore: FirebaseApp(__FIRAPP_DEFAULT)
flutter: storage: null
I am really stumped as to what might cause this partial failure of Firebase to initialize. I've tried changing the access rules in the Firebase console but this has no effect. Below I've pasted the section of my pubspec.yaml file having do do with Firebase includes.
# Firebase
firebase_core: ^0.4.0+9
firebase_analytics: ^5.0.2
firebase_auth: ^0.14.0+5
cloud_firestore: ^0.12.9+5
firebase_storage: ^3.0.8
Also, here is the code that makes the request to the Storage API. Again, I don't think this is the issue because the instance is null from the beginning, but here it is for the sake of completeness:
// saves a locally stored image at local_uri as name. After the storage task
// is complete, the DocumentReference ref is updated with the new url
Future<void> _save_storage(String name, String local_uri, DocumentReference ref) async {
StorageReference storage_reference;
storage_reference = FirebaseStorage.instance.ref().child('images/$name');
final StorageUploadTask upload_task = storage_reference.putFile(io.File(local_uri));
final StorageTaskSnapshot download_url = (await upload_task.onComplete);
final String url = (await download_url.ref.getDownloadURL());
// now, update the file in the database to point to the correct storage
// url
Firestore.instance.runTransaction((transaction) async {
await transaction.update(ref, {'image_url' : url});
});
}
Frustratingly, this issue throws no exceptions. Files are simply not uploaded to the bucket. I know that the instance fields are uninitialized by doing some random probing in debug mode. Also, I have found that the above StorageUploadTask instance has an error code of -13000, but I have not found any documentation on this error code anywhere.
I am very new to both Flutter and Firebase, so I think part of the issue here is that I don't understand how Firebase instances are created. I would appreciate any guidance on this issue. Thank you.
allow read, writeis the only rule. - Sam Cohen