getting xml value loop inside container - flutter

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);
}
});

Related

Flutter List after storing all data in a for-loop, when out of loop replace all stored records with the last record. Why's that?

I want to pass data from json to a list. I load the list with data into a for-loop, and it works as expected, loading 6 records into my list. But when I verify when for-loop is done, my list keep only the last data in all 6 records. Why's that?
This is my code:
class LoadData {
loadSalesData() async {
String optiune = 'gen';
String numeServer = Global.server;
String tabel = Global.tabel;
SalesData sd = SalesData("", 0.00);
List<SalesData> sData = [];
Loader kk = Loader();
kk
.searchOnServer(tabel: tabel, numeServer: numeServer, optiune: optiune, dataInceput: "2022-06-30", dataSfarsit: "2022-07-23")
.then((rezultat) async {
try {
rezultat = "[" + rezultat + "]";
var _json = jsonDecode(rezultat);
Global.dataChart = [];
for (int i = 0; i < _json.length; i++) {
// window.alert(_json[i]['partener'] + " Lenght:" + _json.length.toString());
sd.partener = _json[i]['partener'];
sd.sales = double.parse(_json[i]['Sales']);
Global.dataChart.add(sd);
//This show the list correctly with all data from server
window.alert("Into for-loop $i:" + Global.dataChart[i].partener);
}
//This shows only the last record no matter the List index (i.e. [0],[1]...etc
window.alert("Outside for-loop: " + Global.dataChart[0].partener);
//This shows the same value as the previous
window.alert("Outside for-loop: " + Global.dataChart[1].partener);
} catch (e) {
print(e);
}
});
}
}
//Global.dataChart is defined in a separate class as static List<SalesData> dataChart = [];
//and class SalesData is..
class SalesData {
String partener;
double sales;
SalesData(this.partener, this.sales);
}
It is because you're editing the same instance of SalesData sd and dart uses call by reference on Objects of non-primitive types.
Put SalesData sd = SalesData("", 0.00); in the loop instead.
Like so:
for (int i = 0; i < _json.length; i++) {
SalesData sd = SalesData("", 0.00);
// window.alert(_json[i]['partener'] + " Lenght:" + _json.length.toString());
sd.partener = _json[i]['partener'];
sd.sales = double.parse(_json[i]['Sales']);
Global.dataChart.add(sd);
//This show the list correctly with all data from server
window.alert("Into for-loop $i:" + Global.dataChart[i].partener);
}
To easily reproduce and better understand, try this:
void main() {
final x = X("random");
final l = [];
for(int i =0; i < 3; i ++){
x.a = i.toString();
l.add(x);
}
l.forEach((e) => print(e.a));
}
class X{
String a;
X(this.a);
}

How to get length of String in flutter and display in firestore array

I need to create an array of information into firestore and I want to achieve this:
I have tried this:
Future uploadData(String name) async {
List<String> splitList = name.split('');
List<String> indexList = [];
for (int i = 0; i < splitList.length; i++){
for (int y = 1 ; y < splitList[i].length + 1; y++) {
indexList.add(splitList[i].substring(0,y).toLowerCase());
}
name = name + name[i];
indexList.add(name);
}
print(indexList);
FirebaseFirestore.instance.collection('Users').doc(_auth.currentUser!.uid).update({
'arrayUser': indexList
});
}
but it crash the application...
Hi have found a way to achieve what I needed.
Hope that can help the community:
Future nameArray(String name) async {
List<String> arrName = [];
String s = name.toLowerCase();
String e = '';
for (int i = 0; i < s.length; i++) {
e = e + s[i];
arrName.add(e);
}
FirebaseFirestore.instance.collection('Users').doc(_auth.currentUser!.uid).update({
'userArray': FieldValue.arrayUnion(arrName)
});

How to clone a List inside a for Loop in dart

class MarketStyleTwoOutcome extends StatelessWidget {
MarketStyleTwoOutcome(this.markets) : marketList = markets.markets;
final Markets markets;
final List<Market> marketList;
List<Market> marketNewList = [];
#override
Widget build(BuildContext context) {
List market = []..addAll(markets.markets);
List<Outcome> marketOutcomes = markets.markets[0].outcomes;
for (var i = 0; i < 3; i++) {
marketNewList[i] = []..addAll(markets.markets);
}
so my goal is to duplicate markets.markets 3 times, but inside my for loop it seems that I have a code error. I also did try
for (var i = 0; i < 3; i++) {
marketNewList.add([]..addAll(markets.markets)); //
}
and
for (var i = 0; i < 3; i++) {
marketNewList.add(List.from(markets.markets)); //
}
but it seems not to work
Using the spread operator is a good way to do this. Try something like this:
void main() {
final list = ['hello', 'world'];
var newList = [];
for (int i = 0; i < 3; i++) {
newList = [...newList, ...list];
}
print(newList);
}

variable doesn't get value from for loop

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;
});

Flutter - Function - Return Array

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.