Problem with filling the list after await command - flutter

I have just started Flutter and I am stuck at a point. Below is some part of my function that first uploads provided pictures on firebase storage and then get their downloadable URLs and store them in the list of string. It should upload data to firestore with the list of URLs obtained from command list.add(imageUrl);
List<String> list = [];
imgList.forEach((element) async {
final String filePath = element.path;
final res =
filePath.substring(filePath.lastIndexOf('im'), filePath.length);
final ref = FirebaseStorage.instance.ref().child('images').child(res);
await ref.putFile(File(element.path));
final imageUrl = await ref.getDownloadURL();
list.add(imageUrl);
});
final doc = FirebaseFirestore.instance.collection('cars').doc();
but it doesn't happens because the following line is called before filling the list due to await.
await doc.set({
'id': doc,
'img': list,
'price': 14450,
'make': 'Toyota',
'year': 2015`
});

Add await before imgList.forEach like
List<String> list = [];
await imgList.forEach((element) async {
final String filePath = element.path;
final res =
filePath.substring(filePath.lastIndexOf('im'), filePath.length);
final ref = FirebaseStorage.instance.ref().child('images').child(res);
await ref.putFile(File(element.path));
final imageUrl = await ref.getDownloadURL();
list.add(imageUrl);
});
final doc = FirebaseFirestore.instance.collection('cars').doc();
edit
if this doesn't work then you may use for loop instead of forEach like
List<String> list = [];
for(var element in imgList) {
final String filePath = element.path;
final res =
filePath.substring(filePath.lastIndexOf('im'), filePath.length);
final ref = FirebaseStorage.instance.ref().child('images').child(res);
await ref.putFile(File(element.path));
final imageUrl = await ref.getDownloadURL();
list.add(imageUrl);
}
final doc = FirebaseFirestore.instance.collection('cars').doc();

Related

Issues with flutter futures - map results in List<Future>

I'm having issues with the return type of this HTTP request, I need to return Future<List> but the map is returning a List<Future>. I don't know how to get the CustomerInfo without it being a future and I don't know how to return it any other way.
if (response.statusCode == 200) {
Iterable l = json.decode(response.body);
final data = List.from(l.map((model) async {
final name = model['UserName'];
final id = model['UserICode'];
return {name, id};
}));
final users = data.map<CustomerInfo>((e) async {return await getFaxinfo(e.id, e.name);});
return users;
} else {
throw 'err';
}
}
Future<CustomerInfo> getFaxinfo(
String id,
String name,
) async {
final baseUrl = 'localhost';
final int port = 3003;
final accountsPath = '/accounts';
final accountsFaxInfoPath = '$accountsPath/fax-info';
final Map<String, dynamic> queryParam = {'id': id};
final uri = Uri(
scheme: 'http',
path: accountsFaxInfoPath,
host: baseUrl,
queryParameters: queryParam);
final response = await http.get(uri);
return CustomerInfo(sent: 200, received: 300, name: 'Test');
}
The problem is that an async function always returns a Future no matter if you call await inside it or not. To fix it a good approach is to use list comprehension. A simple for loop also would do.
Instead of those 2 maps that result in List<Future>:
final data = List.from(l.map((model) async {
final name = model['UserName'];
final id = model['UserICode'];
return {name, id};
}));
final users = data.map<CustomerInfo>((e) async {return await getFaxinfo(e.id, e.name);});
Do the following with list comprehension:
final users = [
for (final model in l)
await getFaxinfo(model['UserICode'], model['UserName'])
];
Now if you want to make the HTTP calls in parallel it's possible to do a Future.wait() in a List<Future> to get the result as List<CustomerInfo>. Something like the following:
final users = await Future.wait(l.map((model) async =>
await getFaxinfo(model['UserICode'], model['UserName'])));

Save ImagePicker Image in Shared Preferences - Flutter

I'm trying to save image picked from ImagePicker and store it in shared preferences and then retrieve it from there but no luck so far
To make my question more specific, how to save an image as a string in shared preference and then later retrieve it
Here is my code
File? profileImage;
void saveData(String key, String value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(key, value);
}
void getData(String key) async {
final prefs = await SharedPreferences.getInstance();
final image = await prefs.getString(key);
setState(() {
profileImage = image; //this would result into error because profileImage expect file type value
});
}
Future pickProfile() async {
final profileImagePicker = await ImagePicker().pickImage(source: ImageSource.gallery);
final File profile = File(profileImagePicker!.path);
final directoryPath = await getApplicationDocumentsDirectory();
final path = directoryPath.path;
final imageFile = await File(profileImagePicker.path).copy('$path/image1.png'); // What am I supposed to do after this step
saveData('profile', path); what value needs to be stored here, it expects a string
setState(() {
profileImage = profile;
});
}
To convert image into String you can use below code
final bytes = imageFile.readAsBytesSync();
String imageString = base64Encode(bytes);
To convert String to Image
Uint8List bytes = BASE64.decode(base64ImageString);
You can use the Image widget to diplay the Image
Image.memory(bytes);

Flutter 'File' can't be assigned to 'XFile'

I have a function to save network image to local cache files, but I have a trouble when store the list file that I downloaded to List<XFile>. Here is my download function:
List<XFile>? imageFileList = [];
Future<File> downloadImage(url, filename) async {
var httpClient = HttpClient();
try {
var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
final dir = await getTemporaryDirectory();
File file = File('${dir.path}/$filename');
await file.writeAsBytes(bytes);
print('downloaded file path = ${file.path}');
return file;
} catch (error) {
print('download error');
return File('');
}
}
is there any way so I can save the file to imageFileList as :
imageFileList!.add(file);
Convert the file to XFile:
XFile file = new XFile(file.path);
Change your list type:
from this:
List<XFile>? imageFileList = [];
to this:
List<File>? imageFileList = [];
final ImagePicker _picker = ImagePicker();
getImage() async {
var images = await _picker.pickMultiImage();
images!.forEach((image) {
setState(() {
_imageFileList!.add(File(image.path));
});
});
}

Get multiple Image download URLs from Firebase Storage- I can successfully upload multiple images

I am trying to upload multiple images to firebase storage at once. I can upload successfully but cannot retrieve the download URLs;
Future<void> saveImages() async {
int index = images.length;
setState(() {
isLoading = true;
});
try {
final ref = FirebaseStorage.instance
.ref()
.child('images/${FirebaseAuth.instance.currentUser!.uid}');
images.forEach((element) async {
final file = await element.file;
final refPut = ref.child('$element$index');
await refPut.putFile(
File(file!.path),
);
});
final ref2 = ref.child('${imagePciker.path.split('/')}');
await ref2.putFile(File(imagePciker.path));
final ListResult listData = await ref.listAll();
final data =await ref2.getDownloadURL();
print('Im HERE!=Line 95');
Future.forEach(listData.items, (Reference element) async {
await element.getDownloadURL().then((value) => listUrls.add(value));
print('line 101');
print(listUrls.length);
});
print('Line 104');
await FirebaseFirestore.instance.collection('test').add({
'titleDownloadUrl': data,
'ListOfDownloadUrl': listUrls,
});
print('Line 108');
setState(() {
isLoading = false;
});
} catch (e) {
/* setState(() {
isLoading = false;
}); */
print(e);
}
}
The print statements are to debug. This whole function returns and no errors are thrown.
However in the firebase collection 'test' the URLs are not stored correctly.
On the first/fresh run after restart of the app.
the array of ListOfDownloadUrl is empty
On a hot restart(with the no images saved in firebase storage)
the array of ListOfDownloadUrl has one URL
then on multiple restarts, the amount of URLs saved becomes huge(ie. 4 images uploaded-10 URLs saved).
Please comment if I need to add anything else;
Thanks for any Help
.forEach() is NOT ASYNCHRONOUS - it WILL NOT wait. Use await Promise.all(images.map()) (thus creating an array of promises, which must all resolve).
Solved!
Following the thinking of #LeadDreamer
The forEach isn't ASYNCHRONOUS, the problem with his answer is that flutter does not have
Promise.all()
What it does have if Future.wait(), which acts(in my case) the same way.
Future<void> saveImages() async {
int index = images.length;
setState(() {
isLoading = true;
});
try {
final refMain = FirebaseStorage.instance
.ref()
.child('images${FirebaseAuth.instance.currentUser!.uid}');
AssetEntity eleId =
new AssetEntity(id: '', typeInt: 5, width: 5, height: 5);
await Future.wait(images.map((element) async { <-- here instead of the for each
setState(() {
eleId = element;
});
final file = await element.file;
final refPut = refMain.child('suportImage$_counter/$element$index');
await refPut.putFile(
File(file!.path),
);
}));
final ref2 =
refMain.child('headingImage$_counter/${imagePciker.path.split('/')}');
await ref2.putFile(File(imagePciker.path));
final ListResult listData =
await refMain.child('suportImage$_counter').listAll();
final data = await ref2.getDownloadURL();
await Future.wait(listData.items.map((e) async { <-- here instead of for each
await e.getDownloadURL().then((value) => listUrls.add(value));
}));

flutter add value to list

i set object like this : {"name":"alex","code":"123"}
into sharedPrefrence Calss A:
var resBody = {};
resBody["name"] = name.text;
resBody["code"] = pass.text;
str = json.encode(resBody);
print(str);
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("list_customer", str);
and when get this sharedPrefrence in another class
add value of shared to the list Class B:
customer = (prefs.getString('list_customer'));
Map<String, dynamic> user = jsonDecode(customer);
_customer.nameFamily = user['name'];
_customer.code = user['code'];
_list.add(_customer);
and i want to know how can i set new value of shared into the previous list like this :
[{"name":"alex","code":"123"},{"name":"john","code":"128"}]
To store multiple customers, you need a List not Map.
Declare a List
List<Map<String, dynamic>> customers = [];
Add object(s) to the list
final customer = {
"name": name.text,
"code": pass.text,
};
customers.add(customer);
Stringfy (encode) customers list
final customersString = json.encode(customers);
Store encoded customers list
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("list_customer", customersString);
Let's say you store one customer, and now you need to store another.
we get customers list first, then we decode it
String customersString = await prefs.getString('list_customer');
final customers = json.decode(customersString);
Add the new object to the list (previous step #2)
customers.add({
"name": 'Amir',
"code": 'SWF2022',
});
Repeat step #3 and #4.
Good luck
Check example below
Future saveGetValues() async {
const key = 'list_customer';
var list = <Map<String, String>>[];
list.add(createValue('name1', 'code2'));
//save first time
save(key, list);
list = await getValue<List<Map<String, String>>>(key); //here your saved list
//add second value
list.add(createValue('name2', 'code2'));
save(key, list);
list = await getValue<List<Map<String, String>>>(key); //here your saved list with two items
}
Map<String, String> createValue(String name, String code) {
final resBody = <String, String>{};
resBody["name"] = name;
resBody["code"] = code;
return resBody;
}
Future save(String key, dynamic value) async {
final prefs = await SharedPreferences.getInstance();
prefs.setString(key, jsonEncode(value));
}
Future<T> getValue<T>(String key) async {
final prefs = await SharedPreferences.getInstance();
return json.decode(prefs.getString(key)) as T;
}