[英]Can I Use "FutureBuilder()" under Void Method in Flutter
This is My Code .这是我的代码。
Future<void> SendOrderDetails() async{
Row(
children: [
FutureBuilder(
future: topcart.getData(),
builder: (context, AsyncSnapshot<List<Cart>> snapshot) {
for(int i = 0; i<itemcount; i++)
{
if(itemcount>listModel2.data!.length) {
listModel2.data?.add(Model2(
ORDER_INFO_ID: 1,
PRODUCT_ED_ID: 2,
QTY: quantitycalcule.toString(),
UNIT_PRICE:'00',// snapshot.data![i].Book_initional_price!.toString(),
CHGED_BY:1,
CHGED_DATE: DateTime.now().toString(),
STATUS: 'P',
),);
}
}
return const Text('');
}
),
],
);
}
When I Call This, "FutureBuilder" did not run.当我调用它时,“FutureBuilder”没有运行。 I need "snapshot" in If condition.我需要 If 条件下的“快照”。 Please Help me.请帮我。
I'm not sure what your code is trying to accomplish but there are a few things I can see that could be potentially causing issues:我不确定您的代码试图完成什么,但我可以看到一些可能会导致问题的事情:
The whole point of a FutureBuilder is to builds itself based on the latest snapshot of interaction with a Future. FutureBuilder 的全部意义在于根据与 Future 交互的最新快照来构建自己。 Maybe you can explain why this structure is the way it is.也许你可以解释为什么这个结构是这样的。
// This needs to be outside the build method, maybe initState()
Future<TYPE> _gd = topcart.get()
// Future Builder in the build method
FutureBuilder<TYPE>(
future: _gd,
builder: (BuildContext context, AsyncSnapshot<TYPE> snapshot) {});
if (snapshot.connectionState == ConnectionState.waiting) {
return // Progress indicator widget
} else if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return // Error Widget or Text
} else if (snapshot.hasData) {
return // Process data here
} else {
return // Empty set returned
}
} else {
return Text('State: ${snapshot.connectionState}');
}
this method returns nothing so you don't have way to check whether the future builder run or not, try to use print and check your debug console (or try to debug your code by using break points) if it works then do what ever you want to do additional question: what state management you are using?此方法不返回任何内容,因此您无法检查未来的构建器是否运行,请尝试使用打印并检查您的调试控制台(或尝试使用断点调试您的代码)如果它有效然后做任何你想做附加问题:你用的是什么state管理?
The whole point of FutureBuilder is to build (and so to return) a Widget based on a snapshot. FutureBuilder 的全部意义在于构建(并返回)基于快照的 Widget。
As it seems you don't need this Widget at all in your code, couldn't you just skip the Builder altogether?看起来您的代码中根本不需要这个 Widget,难道您不能完全跳过 Builder 吗? Something like this:是这样的:
Future<void> SendOrderDetails() async {
var data = await topcart.getData();
for (int i = 0; i < itemcount; i++) {
if (itemcount > listModel2.data!.length) {
listModel2.data?.add(
Model2(
ORDER_INFO_ID: 1,
PRODUCT_ED_ID: 2,
QTY: quantitycalcule.toString(),
UNIT_PRICE: data![i].Book_initional_price!.toString(),
CHGED_BY: 1,
CHGED_DATE: DateTime.now().toString(),
STATUS: 'P',
),
);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.