参数类型'Future<int?>'不能分配给参数类型"int"



我想从count中获取数据作为int。当我调用函数ItemCount.getTotalOrder((时,它会显示此错误。请帮忙。

我的独立项目计数类函数:

class ItemCount {
static final db = FirebaseFirestore.instance.collection('Orders');
static Future<int?> getTotalOrder() async {
final count = await db.get().then((value) {
return value.docs.length;
});
print(count);
if (count == null) {
return 0 ;
} else {
return count;
}
}
}

数据类型为int的函数调用:

Expanded(
flex: 2,
child: _buildTile(
title: AppString.totalOrders,
data: ItemCount.getTotalOrder(),
color: blueColor,
),

ItemCount.getTotalOrder()是一个异步函数。这意味着您需要等待将来返回数据。为此,使用FutureBuilder包装小部件。

FutureBuilder(
future: ItemCount.getTotalOrder(),
builder: (context, snapshot) {
if (snapshot.hasData) {  //true when the data loading is complete from the async method
return Expanded(
flex: 2,
child: _buildTile(
title: AppString.totalOrders,
data: snapshot.data,  //snapshot data contains the data returned from future
color: blueColor,
);
} else {
return CircularProgressIndicator();  //FutureBuilder will be displaying this widget as long as the data returned from future is null (that is, still loading)
}
}
)

相关内容

最新更新