Flutter and Firebase: Return all images from Firebase Storage - firebase

I have seen a few articles written online by people pertaining to be able to do this, but they only tell you how to do this with a specific, controlled list of images where they also know all the filenames beforehand.
There is also this "answer" posted here: Flutter - Get all images from firebase storage which does not actually resolve this issue at all as it suggests a .listAll() method for the recommended plugin, but there is no such method as .listAll() using the suggested plugin.
I need to be able to not know how many images are in Firebase storage, or what they might be called, just to return everything stored there.
UPDATE:
So because Firebase is full of so many limitations that it hardly qualifies as a database at all, it seems we may have to keep the images in Firebase Storage and a reference list of these in a Firebase Realtime Database Document.
I am stuck on the actual implementation of this however, as I am not even sure firstly how best to go about this. What I am attempting to do is store all the Storage image URLs in an array (if this is not the best way to do this, let me know!):
Firebase Structure
collection -> document -> fields
userData profileImages URLs (array)
My first issue is that I don't know how to append new data to the existing array, it seems to just overwrite it each time I add a new item so that I only ever have one string in the array in the database:
Firestore.instance.collection('userData').document('profileImages').updateData({
'URLs': _uploadedFileURL,
});
Then after this I am also not sure how to actually retrieve the full array of data later when I need it:
Container(
child: StreamBuilder(
stream: Firestore.instance.collection('userData').document('profileImages').snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return LoadingAnimationBasic();
}
if (snapshot.data.data == null) {
return LoadingAnimationBasic();
} else {
return ListView(
shrinkWrap: true,
children: _buildProfileGallery(snapshot),
);
}
},
),
),
And then the function:
_buildProfileGallery(AsyncSnapshot<DocumentSnapshot> snapshot) {
int test = snapshot.data["URLs"].length;
print('URLs in List: ' + test.toString());
return snapshot.data.data.map(???);
}
I have no idea what to put as the parameters of this map, as the hint text is insane:
MapEntry(K2, V2> f(String key, V value)
I can't even begin to guess what this means.
Am I on the right track? Am I on the right planet?

The ability to list files was added to Firebase's client-side SDKs for Android, iOS and JavaScript last year, but has not yet landed in the FlutterFire bindings.
The answer to the question you linked, refers to a pull request on the FlutterFire open-source project that adds this functionality. So while somebody wrote code to allow listing of files in Flutter too, this code hasn't been added to a release so far. So the only way to use that code now, is to build your own version of the FlutterFire library for firebase_storage.
Without the ability to list files in the Firebase Storage API, you'll have to revert back to what everyone did before this feature was added: keeping a list of the files in a secondary location, such as the Firebase Realtime Database or Cloud Firestore.

Related

How to update a list automatically using Firestore Stream?

In my app, I am now using a "refresh function" to update a list in Provider. When the user swipe, I call Refreshlist in my provider and with NotifyListeners() it updates my UI just fine. (The UI is linked to the list _myEleves).
I am afraid that users might use this "refresh" button too many times making unnecessary calls and "reads" on firebase and so increasing artificially the number of reads so the costs of Firebase.
Here is the code :
Future<void> refreshEleveList() async {
final DocumentSnapshot<Map<String, dynamic>> docInfo =
await FirebaseFirestore.instance
.collection('familyAccounts')
.doc(_accountEmail.toLowerCase())
.get();
_mesEleves = (docInfo['mesEleves'] as List<dynamic>)
.map((data) => Eleve.fromMap(data))
.toList();
notifyListeners();
}
I have been reading about STREAMS a lot, but I just can't get it right on how to start this stream, the listening to the changes on Firebase inside my PROVIDER file, so that changes will be made to "_myEleves" list.
What I want to do is that each time a change on firebase happens, it updates my list "_myEleves". Is there a simple way to do this ?
My Provider covers the whole app (I use it in the MAIN file). I thought of adding a StreamProvider, but the thing is I don't want this stream to start until user is authentified etc... and userInfo is first downloaded.
Right now : when user logs in : it downloads from firebase all necessary info, _mesEleves being one of them (This is for a teacher). Whenever a new student joins the group, it modifies firebase and so it should stream down this info into "myEleves" list on the teacher account.
Anybody can help ?

Flutter gives null error, but works after restart

I am new to Flutter, I am trying to fetch data from firestore collection. I am using FutureBuilder with Provider, but it shows null error on initial & runs perfectly after restarting the app.
Here is the code to fetch the data:
FutureBuilder<QuerySnapshot>(
future: FirebaseFirestore.instance
.collection(collec.collectionProducts)
.get(),
builder: (context, snapshot) {
return Consumer<CartItemCounter>(
builder: (BuildContext context, CartItemCounter cart, _) {
if (cart.checkProductAddedToCart(model.productId)) {
return Row(....);
Error
code for futurebuilder
code for ui
(here i have changed the collection name)
How can i solve it. I have tried every solution available on online. Thank you
Where is the error coming from?
According to this, the issue is coming from your Consumer<CartItemCounter>.
What does the error mean?
This error, The method 'x' was called on null., means that the class where this x function is written, that class is null.
What is the reason for this error in my code?
Your Consumer<CartItemCounter> provides an instance of CartItemCounter by the name cart. Then, you call function checkProductAddedToCart.
The error message is telling you that cart is null which is being given to you by the Consumer widget. This means that Consumer probably also did not find that Provider. I expect that there should be an error log by Provider, telling you that CartItemCounter was not found.
Possible Solution
Try 'providing' CartItemCounter.
How do you do that? Go to the top of your app's widget tree to MaterialApp, wrap it inside a MultiProvider or ChangeNotifierProvider and then pass your new CartItemCounter as value.
Read Official Provider Docs for more info on how to expose a provider.
You need to upload your complete log, so that I can further help you.
Always check the result for the future builder, ie snapshot has data.
Place a switch condition or if-else bock depending on the status of the snapshot.
check the example from the official doc here

Fetching rows with a particular value of child Firebase Flutter

I'm working with Flutter and Firebase (Real-time database). There is some data stored in the db and I want to compare the email (child) of the parent and only want to display the parents containing that particular email. Currently, it is fetching all rows. I think fetching through key value pair would do the work. But I dont know the syntax and unable to find help regarding it. Please help me out.
void myfunc() {
databaseReference.once().then((DataSnapshot snapshot) {
print('Data : ${snapshot.value}');
});
}
try with
yourRef.orderByChild("email").equalTo('abs#abc.com');
Read Query Data

Flutter & Firebase: How to populate an array and then later, return all the contents

I have been trying to get arrays working in Firebase, and I am aware that there are a lot of references and discussions about this online, and I have read through all of these and none of it works.
First off, the Firebase side. The structure containing the array and two example strings inside it:
Firebase Structure
collection -> document -> fields
userData profileImages URLs (array)
: https://firebasestorage.googleapis.com/v0/b/app-138804.appspot.com/o/jRwscYWLs1DySLMz7jn5Yo2%2Fprofile%2Fimage_picker4459623138678.jpg?alt=media&token=ec1043b-0120-be3c-8e142417
: https://firebasestorage.googleapis.com/v0/b/app-138804.appspot.com/o/jRwscYWLs3872yhdjn5Yo2%2Fprofile%2Fimage_picker445929873mfd38678.jpg?alt=media&token=ec3213b-0120-be9c-8e112632
The first issue I am facing is writing to this array in the database:
Firestore.instance.collection('userData').document('profileImages').updateData({
'URLs': _uploadedFileURL,
});
Whenever I add data to this array, it just overwrites the existing data. I need to be able to keep all the existing data intact and simply add the current new line to the array.
Once this is working, I then need to be able to return all of the strings in this array without needing to know how many of them there will be.
For this part, I basically have nothing at this point. I could show some of the things I have tried based on suggestions from other articles on this, but none of it is even close to working correctly.
im assuming that _uploadedFileURL is a String, and you are updating the property URLs, that's why your data gets overwritten, because you are changing the URLs value to a single string which is _uploadedFileURL. to solve this issue, simply get the current data inside profileImages before commiting the update. like so
final DocumentSnapshot currentData = await Firestore.instance.collection('userData').document('profileImages').get();
Firestore.instance.collection('userData').document('profileImages').updateData({
'URLs': [
...currentData.data['URLs'],
_uploadedFileURL
],
});
and for the second part of your question, all you need is to query for the profileImages
Future<List<String>> _getProfileImages() {
final document = Firestore.instance.collection('userData').document('profileImages').get();
return document.data['profileImages]
}
the result of the get method will be a DocumentSnapshot, and inside the data property will access the profileImages which is a List<String>.
Ok guys and girls I have worked this out. Part 1: appending data to an array in Firebase.
Firestore.instance.collection('userData').document('profileImages').updateDataupdateData({
'URLs':FieldValue.arrayUnion([_uploadedFileURL]),
});
Where _uploadedFileURL is basically a string, for these purposes. Now I have read that arrayUnion, which is super groovy, is only available in Cloud Firestore, and not the Realtime Database. I use Cloud Firestore so it works for me but if you are having issues this might be why.
Now what is extra groovy about Cloud Firestore is that you can similarly remove an element from the array using:
Firestore.instance.collection('userData').document('profileImages').updateDataupdateData({
'URLs':FieldValue.arrayRemove([_uploadedFileURL]),
});
So how to get this data back out again. A simple way I have found to get that data and chuck it into a local array is like so:
List imageURLlist = [];
DocumentReference document = Firestore.instance.collection('userData').document('profileImages');
DocumentSnapshot snapshot = await document.get();
setState(() {
imageURLlist = snapshot.data['URLs'];
});
From here at least you have the data, can add to it, can remove from it and this can be a platform for you to figure out what you want to do with it.

Firestore snapshots.map(_transform) has no data in StreamBuilder

I have a Flutter project that uses Cloud Firestore. In one of my widgets, I have a StreamBuilder that ideally reads snapshots from the database. In theory, my API for reading from a remote server should be abstract enough to swap Firestore out with a different implementation.
class Database {
Stream<QuerySnapshot> get snapshots => Firestore.instance.collection('entires').snapshots();
Stream<List<String>> get entries => snapshots.map((snapshot) => snapshot.documents.map((document) => document.data['name']).toList());
}
If my StreamBuilder uses snapshots, then AsyncSnapshot<QuerySnapshot> has data (hasData returns true).
If my StreamBuilder uses entries, then AsyncSnapshot<List<String>> will never have data (hasData returns false)---even if I successfully printed out what the data is (my return result is a populated list of strings).
I hope to keep my Database interface free of Firestore. So, my question is: why does StreamBuilder's AsyncSnapshot return nothing even if I have data?
It seems that the issue revolves around snapshot() sending data off immediately. I managed to create a work around by creating a class that wraps my Firestore collection.
This wrapper has its own StreamController<T> where T is the non-Firestore (abstract across multiple backend implementations) data type that I want to return.
The wrapper will listen on the necessary Firestore snapshots on construction. The latest snapshot result is cached. My StreamBuilder can then use both the internal StreamController.stream and the cached snapshot data for its construction.
StreamBuilder(
stream: _wrapper.stream,
initialData: _wrapper.latestData,
)
This works for now, but has the side effect of continuously listening on snapshots indefinitely. I'll have to update my wrapper to stop listening on snapshots when not necessary (no subscribers) and re-listen when there are new subscribers, or simply use a mapping of the snapshot stream.

Resources