Want to wait for the future object before proceeding in flutter? Start fetching the list in initState() and call setState when the list is fetched, to do this asynchronously. Below you can find a simplified example of this process. Also note the await statement before get Filename. This makes sure you return to that piece of code after is is done executing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | class ListPage extends StatefulWidget { @override _ListPageState createState() => _ListPageState(); } class _ListPageState extends State<ListPage> { // This should actually be a List<MyClass> instead of widgets. List<Widget> _list; @override void initState() { super.initState(); _fetchList(); } Future _fetchList() async { List<Widget> singleLineImages = new List(); List unit; for (int i = 0; i <= widget.unitsList.length-1; i++){ for (int j = 1; j <= int.parse(widget.unitsList[i].quantity); j++){ print("${widget.unitsList[i].bulletin}, ${widget.unitsList[i].mountType}, ${widget.unitsList[i].disconnect}"); String fileName = await getfileName(widget.unitsList[i].bulletin, widget.unitsList[i].mountType, widget.unitsList[i].disconnect); singleLineImages.add( Image.asset("images/SD_Files_2100/$fileName.jpg", height: 400.0, width: 200.0,)); } } // call setState here to set the actual list of items and rebuild the widget. setState(() { _list = singleLineImages; }); } @override Widget build(BuildContext context) { // Build the list, or for example a CircularProcessIndicator if it is null. } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.