Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

error in passing arguments and list duplicating passed items two times in flutter

I am using arguments method in Navigator to pass a List

Navigator.pushNamed(context, '/cam', arguments: {'label' : list});

list is a string of items separated by comma for eg: item1, item2

and on receiving the the map of data from the first screen in second screen, I store that in a List by

data = ModalRoute.of(context).settings.arguments;
print(data);
rekognition.add(data['label']);
print(rekognition);

under Widget build and the above print statements prints [null, item1, item2] and [null, item1, item2, null, item1, item2] respectively.

this is where the problem is I don't why null pop up here and the list adds all the items second time

also

for (x=0; x<ing.length; x++) {
    list = '$list , ${ing[x]}';
}

ing is again a list and its equal to List<dynamic>();

all I wanted to do was send a list of items which is added in the first screen and or else a empty list to the second screen and add all those received data to another variable in second screen and do object detection and add the label to the list of items that was passed form the first screen and go back to the first screen again with data of the all the list items including the data that was passed and added


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was able to solve null error by sending the list ing as a single variable instead of a map by

arguments: ing.toList() and the reason why the list in second screen adding items second time because of setState() function which on calling will rebuild the full widget tree hence my function of adding the items to list is called again, this was solved by a if condition

if (mylist.length == 0 ) { mylist.add(data); }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...