I am writing a code in flutter in which I am using an SQFlite database. I want to insert image widget from the assets, and I am getting the name of the image from database.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Single Line diagram"),backgroundColor: Colors.red.shade700,),
body: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Align(
//alignment: Alignment.center,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Row(
//crossAxisAlignment: CrossAxisAlignment.center,
children: imageList(),
),
),
)
),
);
}
The above code calls imageList() for the list of images to display.
List<Widget> imageList(){
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}");
getfileName(widget.unitsList[i].bulletin, widget.unitsList[i].mountType, widget.unitsList[i].disconnect);
//if(fileName != null) {
singleLineImages.add(
Image.asset("images/SD_Files_2100/$fileName.jpg", height: 400.0, width: 200.0,));
//}
}
}
return singleLineImages;
}
I am getting the filename from getFileName() method which is using the database.
getfileName(String bulletin, String mountType, String disconnect)async {
fileNameList = await db.getSDfileName(bulletin, disconnect, mountType);
fileName = fileNameList[0]['FileName'];
print("filename: $fileName");
}
Now, after calling the getFileName(), the program is not waiting for the fileName and proceeding further, which takes filename as null. The filename is obtained correctly after the Image.asset code. Is there any way, so that the program waits untill it gets the proper filename?
wait<T> method Null safety Waits for multiple futures to complete and collects their results. Returns a future which will complete once all the provided futures have completed, either with their results, or with an error if any of the provided futures fail.
A future (lower case “f”) is an instance of the Future (capitalized “F”) class. A future represents the result of an asynchronous operation, and can have two states: uncompleted or completed. Note: Uncompleted is a Dart term referring to the state of a future before it has produced a value.
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.
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.
}
}
Sidenote: you are making a lot of calls to the database, which is probably inefficient. Try to get the required data in a single db call. But that is another topic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With