如何在继续颤抖之前等待未来的对象?
How to wait for the future object before proceeding in flutter?
我正在使用 SQFlite 数据库编写 flutter 代码。我想从资产中插入图像小部件,我正在从数据库中获取图像的名称。
@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(),
),
),
)
),
);
}
以上代码调用 imageList() 来获取要显示的图像列表。
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;
}
我从使用数据库的 getFileName() 方法获取文件名。
getfileName(String bulletin, String mountType, String disconnect)async {
fileNameList = await db.getSDfileName(bulletin, disconnect, mountType);
fileName = fileNameList[0]['FileName'];
print("filename: $fileName");
}
现在,调用 getFileName() 后,程序不再等待 fileName 并继续执行,这会将 filename 设为 null。 Image.asset代码后正确获取文件名。 有什么方法可以让程序等待直到获得正确的文件名?
在 initState()
中开始获取列表并在获取列表时调用 setState
,以异步执行此操作。您可以在下面找到此过程的简化示例。还要注意获取文件名之前的等待语句。这可以确保您 return 在执行完那段代码后。
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.
}
}
旁注:您正在对数据库进行大量调用,这可能效率低下。尝试在单个数据库调用中获取所需的数据。但那是另一个话题。
在 class 中保留 Future<Widget> _list;
字段。
在initState()
函数中添加_list = _fetchList();
。
另请注意,在这种情况下,_fetchList
应该 return Future<Widget>
。
在构建函数中使用 FutureBuilder
。
我正在使用 SQFlite 数据库编写 flutter 代码。我想从资产中插入图像小部件,我正在从数据库中获取图像的名称。
@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(),
),
),
)
),
);
}
以上代码调用 imageList() 来获取要显示的图像列表。
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;
}
我从使用数据库的 getFileName() 方法获取文件名。
getfileName(String bulletin, String mountType, String disconnect)async {
fileNameList = await db.getSDfileName(bulletin, disconnect, mountType);
fileName = fileNameList[0]['FileName'];
print("filename: $fileName");
}
现在,调用 getFileName() 后,程序不再等待 fileName 并继续执行,这会将 filename 设为 null。 Image.asset代码后正确获取文件名。 有什么方法可以让程序等待直到获得正确的文件名?
在 initState()
中开始获取列表并在获取列表时调用 setState
,以异步执行此操作。您可以在下面找到此过程的简化示例。还要注意获取文件名之前的等待语句。这可以确保您 return 在执行完那段代码后。
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.
}
}
旁注:您正在对数据库进行大量调用,这可能效率低下。尝试在单个数据库调用中获取所需的数据。但那是另一个话题。
在 class 中保留 Future<Widget> _list;
字段。
在initState()
函数中添加_list = _fetchList();
。
另请注意,在这种情况下,_fetchList
应该 return Future<Widget>
。
在构建函数中使用 FutureBuilder
。