Why is the Value of Response response not getting the value from the inner for loops response? Its value stays ""
Future getArticle() async {
final data1 = await fetchArticles();
Response response = Response("", 200);
for (int i = 0; i == data1.length; i++) {
response = await get(
Uri.parse('https:/url/' +
data1[i]["file"]),
);
return response; //I want it to have this responses value
}
String data2 = (response.body);
return data2;
}
The for loop you post is like this :
for (int i = 0; i == data1.length; i++) {
response = await get(
Uri.parse('https:/url/' +
data1[i]["file"]),
);
This won't trigger the i++ at all, since you are checking if i == data1.length.
Change it to i < data1.length:
for (int i = 0; i < data1.length; i++) {
response = await get(
Uri.parse('https:/url/' +
data1[i]["file"]),
);
This will make sure it fully loops through the entire length of data1.
This loop doesn't make sense. It will always return the first element and exit the loop in which case you don't need a loop. If you want a loop then do this:
data1.forEach((value) => {
response = await get(
Uri.parse('https:/url/' +
value["file"]),
);
return response.body;
});
Related
Here is my code
List<DocumentReference> getStacks(
List<DocumentReference> favoritePlayers,
List<DocumentReference> stacks,
) {
List<DocumentReference> newStacks = [];
for (var i = 0; i < stacks.length; i++) {
stacks[i].get().then((DocumentSnapshot stackSnapshot) {
if (stackSnapshot.exists) {
DocumentReference thisstack = stackSnapshot['player'];
for (var x = 0; x < favoritePlayers.length; x++) {
if (favoritePlayers[x] == thisstack) {
newStacks.add(stacks[i]);
}
}
}
});
if (stacks.length - 1 == i) {
return newStacks;
}
}
}
I do not have the option in the platform I am using to make the function async or Future, so I am trying to figure out a way to not return until both for loops are done. I have tried while loops and do while, but I guess I am missing something, this method is close, but it seems it still finishes before the inner loop does and wants to return nothing.
Any thoughts would be great.
In your code, the loop goes to the next operation without waiting for "stacks[i].get()" .
If the "stacks[i].get()" function is not a Future function, you can use it as follows.
List getStacks( List favoritePlayers, List stacks) {
List newStacks = [];
for (final stack in stacks) {
DocumentSnapshot stackSnapshot = stack.get();
//code
newStacks.add(stackSnapshot);
}
return newStacks;
}
If it is a future function, it should be like this;
Future<List> getStacks( List favoritePlayers, List stacks) async{
List newStacks = [];
for (final stack in stacks) {
DocumentSnapshot stackSnapshot = await stack.get();
//code
newStacks.add(stackSnapshot);
}
return newStacks;
}
i create those loops and thats work inside loops but how i can use those loop in a container ?
i want getting category in a container with nombers of categorys and the values for each category
the same thing for items
final url = "https://www.comptoir-hardware.com/home.xml";
Future<void> rssz() async {
//lire un fichier XML
final response = await get(Uri.parse(url));
setState(() {
String body = utf8.decode(response.bodyBytes);//ligne pour decodage en arabe
var channel = RssFeed.parse(body);
feed = channel;
titreprincipal = feed.title.toString();
linkprincipal = feed.link.toString();
descriptionprincipal = feed.description.toString();
authorprincipal = feed.author.toString();
for (int j = 0; j < feed.categories!.length; j++) {
final docs = feed.categories![i];
print(docs.value);**//the value not work :/ but the look j works if i do print(j); i can see the number of category, so how i can get the value too ?**
}
for (int i = 0; i < feed.items!.length; i++) {
final item = feed.items![i];
print(item.link);
}
});
I am trying to call Distance Matrix API and every time I call it, it returns an invalid request, this is my code for calling request and parsing it
output:
{destination_addresses: [], origin_addresses: [], rows: [], status: INVALID_REQUEST}
the function of requesting from the api:
Future<String> getDuration(LatLng l1, List<LatLng> l2) async {
var destinations = await _getwaypoints(l2);
String url =
"https://maps.googleapis.com/maps/api/distancematrix/json?
origin=${l1.latitude},${l1.longitude}&destination=$destinations&departure_time=now&key=$apiKey";
http.Response response = await http.get(url);
Map values = jsonDecode(response.body);
return values.toString();
_getwaypoints function which i use to format the list to be suitable for the request:
Future<String> _getwaypoints(List<LatLng> way) async {
String waypointcoords = '';
// Future<String> finalwaypoints;
if (way == null) {
return null;
} else {
for (int i = 0; i <= (way.length - 1); i++) {
var lat = way[i].latitude.toString();
var lon = way[i].longitude.toString();
if (i == 0) {
waypointcoords += '$lat%2C$lon';
} else {
waypointcoords += '%7C$lat%2C$lon ';
}
}
waypointcoords.trim();
return waypointcoords;
}
}
also, I tried to pass one origin location and one destination location and it was the same output,
why the return is invalid request statues while it still recognizes that its distance matrix API and returning empty destination_addresses, origin_addresses, and rows,
is there something I am doing wrong?
In the request URL, instead of origin and destination it must be origins and destinations
In this sample for statement, I can find out the index when if statement returns true:
final List<Cart> cart = box.values.toList();
int ix = 0;
for (int i = 0; i <= cart.length - 1; i++) {
if(cart[i].id == products[productIndex].id){
ix = i;
}
}
Now, my question is: how can I implement this code with .map or .forEach?
int ix = cart.forEach((cart)
{
cart.id == widget.storeCategories[index].products[productIndex].id ? ++ix:ix;
}
);
You can do something like this.
final List<int> list = [1,2,3,4,5,6,7,8];
list.asMap().forEach((index,item){
print("$index: $item");
});
I've build an List with 44 places:
List<String> departmentdes = new List(44);
after that I've called a function:
postDepartment();
The function is an api call:
postDepartment() async {
final response = await http.get('url');
final jsonresponse = json.decode(response.body);
List<Department> departments = [];
for(var d in jsonresponse) {
Department department = Department(
fid: d["fid"].toString(),
);
departments.add(department);
}
int index = departments.length -1;
for(int i = 0; i<=index; i++) {
departmentdes[i] = departments[i].fid;
}
return departmentdes;
}
After the postDepartment(); I want to print the departmentdes but it always returns null. Why?
i < index
You're already defining index to be length -1
Just a little logic error.
Change your postDepartment to this and see if it helps:
Future<void> postDepartment() async {
final response = await http.get('url');
final jsonresponse = json.decode(response.body);
List<Department> departments = [];
for(var d in jsonresponse) {
Department department = Department(
fid: d["fid"].toString(),
);
departments.add(department);
}
int index = departments.length -1;
for(int i = 0; i<=index; i++) {
departmentdes[i] = departments[i].fid;
}
return departmentdes;
}
Also check if your departments is not null.