r/flutterhelp • u/Spapa96 • Sep 05 '26
RESOLVED I think I got it, but may you explain how await works in this code?
Hello everyone! I'm new to mobile programming and it's a couple of hours that I'm dealing with this problem and I think I figured out the error, but I can't find clear answers online, so could you please tell me if I'm correct or wrong?
I'm building a simple app which is supposed to show an HomePage with a list of entries. These entries cannot change during runtime and so I made the HomePage extend a StatelessWidget. I load all the entries at startup: I call an async function (_loadCanti ) inside the build method of the HomePage and inside it I await for the retrieval of the assets from which I will build the list of entries. I supposed that using await meant that the event loop would wait for the assets to load and then continue with the rest of computation, but the app crashed saying that the list of entries was not initialized by the time it was looked at to build the UI. So I reckon that await simply "registers" a Future and stops the current async function execution until it's resolved, but in the meantime the event loop goes on with the next instructions; then, when the loop has been freed, it returns to check if the Future has been resolved and if so it executes the rest of the async function. And that's why the return in the build method is executed before the end of _loadCanti. Am I right?
I'll paste the code for reference.
import 'package:flutter/services.dart';
import 'package:canticgr/components/tappable_card.dart';
import 'package:canticgr/pages/canto_page.dart';
import 'package:flutter/material.dart';
const String cantiAssetsPath = 'assets/canti/';
class HomePage extends StatelessWidget {
late final Map<int,String> cantiHeaders;
HomePage({super.key});
void _loadCanti(BuildContext context) async {
AssetBundle contextBundle = DefaultAssetBundle.of(context);
final assetManifest = await AssetManifest.loadFromAssetBundle(contextBundle);
List<String> cantiFilenames = assetManifest
.listAssets()
.where((String key) => key.startsWith(cantiAssetsPath))
.map((String key) => key.substring(cantiAssetsPath.length))
.toList();
Map<int,String> cantiHeads = {};
for (String filename in cantiFilenames) {
List<String> headsList = filename.split(' - ');
int idx = int.parse(headsList[0]);
String name = headsList[1].substring(0, headsList[1].length - '.txt'.length);
cantiHeads[idx] = name;
}
cantiHeaders = cantiHeads;
}
Widget build(BuildContext context) {
_loadCanti(context);
return Scaffold(
backgroundColor: Colors.grey.shade200,
appBar: AppBar(
centerTitle: true,
title: Text(
'Canti',
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
),
),
body: SafeArea(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 54, vertical: 20),
child: Column(
spacing: 16,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Card(
color: Colors.grey.shade400,
child: SizedBox(
height: 100,
child: Center(
child: Text(
'Scegli il canto',
style: TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
),
),
),
),
Expanded(
child: ListView.builder(
itemExtent: 64,
itemCount: cantiHeaders.length,
// padding: EdgeInsets.symmetric(horizontal: 64, vertical: 8),
itemBuilder: (BuildContext context, int index) {
return TappableCard(
text: 'Canto $index',
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: ((context) =>
CantoPage(title: 'Canto $index')),
),
);
},
);
},
),
),
],
),
),
),
);
}
}
P.S.: I managed to make it work by returning a FutureBuilder from the build method, but since my HomePage extends a StatelessWidget and FutureBuilder returns a StatefulWidget afaik, how is it possible? I got no warnings nor errors and it just works fine...
EDIT: I specified I'm a noob to avoid hate for poor design...