未来的函数无限重载?颤动



我正在构建一个简单的电子商务应用程序,但当我的未来功能无限重新加载时,我陷入了困境:

Future getCartData() async {
print("function one");
print("this is the token mytoken");
String url = 'https://myurl/apis/getCartItems';
http.Response response = await http.post(Uri.parse(url),
headers: {
'Authorization': "token mytoken",
"Content-Type": "application/json",
},
body: json.encode({
"username": "admin",
}));
print(response.body);
var data = json.decode(response.body);
print("cart data recieved :");
print(data.length);
//set state
setState(() {
lengthData=data.length.toString();
totalPrice=0;
});
return data;
}

我正在使用设置状态,因为一旦数据返回,我就需要设置这个值,但当我删除它时,这个设置状态一次又一次地重新加载;lengthData";以及";totalPrice";。

我的init函数:

initState(){
getCartData();
super.initState();
}

这是我的未来建设者:

child:FutureBuilder(
future: getCartData(),
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
// still waiting for data to come
return Center(
child: CircularProgressIndicator(),
);
} else if (snapshot.hasData &&
snapshot.data.isEmpty) {
return Center(child: Text("No Products"));
;
} else {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (BuildContext context, index) {
List list = snapshot.data;
totalPrice = totalPrice + list[index]['price'];
print("this is the total:$totalPrice");
return Container(
margin: EdgeInsets.only(top: 20),
height: 130,
child: Stack(
children: <Widget>[
Align(
alignment: Alignment(0, 0.8),
child: Container(
height: 100,
margin: EdgeInsets.symmetric(
horizontal: 16.0),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: shadow,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(10),
bottomRight: Radius.circular(
10))),
child: Row(
mainAxisAlignment: MainAxisAlignment
.end,
children: <Widget>[
Container(
padding: EdgeInsets.only(
top: 12.0, right: 12.0),
width: 200,
child: Column(
crossAxisAlignment: CrossAxisAlignment
.start,
children: <Widget>[
Text(
'${list[index]['title']}',
textAlign: TextAlign.right,
style: TextStyle(
fontWeight: FontWeight
.bold,
fontSize: 12,
color: darkGrey,
),
),
Align(
alignment: Alignment
.centerRight,
child: Container(
width: 160,
padding: const EdgeInsets
.only(
left: 32.0,
top: 8.0,
bottom: 8.0),
child: Row(
mainAxisAlignment:
MainAxisAlignment
.spaceBetween,
children: <Widget>[
ColorOption(
Colors.red),
Text(
"₹${list[index]['price']}",
textAlign: TextAlign
.center,
style: TextStyle(
color: darkGrey,
fontWeight: FontWeight
.bold,
fontSize: 18.0),
)
],
),
),
)
],
),
),
Theme(
data: ThemeData(
accentColor: Colors.black,
textTheme: TextTheme(
headline6: TextStyle(
fontFamily: 'Montserrat',
fontSize: 14,
color: Colors.black,
fontWeight: FontWeight
.bold),
bodyText1: TextStyle(
fontFamily: 'Montserrat',
fontSize: 12,
color: Colors.grey[400],
),
)),
child: NumberPicker(
value: 2,
minValue: 1,
maxValue: 10,
onChanged: (value) {
// setState(() {
//   quantity = value;
// });
},
))
])),
),
Positioned(
top: 5,
child: SizedBox(
height: 150,
width: 200,
child: Stack(children: <Widget>[
Positioned(
left: 25,
child: SizedBox(
height: 150,
width: 150,
child: Transform.scale(
scale: 1.2,
child: Image.asset('assets/bottom_yellow.png'),
),
),
),
Positioned(
left: 50,
top: 5,
child: SizedBox(
height: 80,
width: 80,
child: Image.network(
'$Imagename',
fit: BoxFit.contain,
)),
),
Positioned(
right: 30,
bottom: 25,
child: Align(
child: IconButton(
icon: Image.asset('assets/red_clear.png'),
onPressed: (){
deleteCartData('${list[index]['id']}');
},
),
),
)
]),
)
),
],
),
);
}
):Center(child: Text("no data"));
}
},
)

有人能帮助我并建议我一个更好的方法来解决它吗<3.提前感谢<3.

您需要从"getCartData(("方法返回一个Future,现在您只返回一个Hashmap,而不是futureBuilder需要的Future。你可以这样做:

return Future.value(data);

还有一件事是,您必须只传递对方法的引用,而不调用它,如下所示:

future: getCartData,

此外,请指定返回类型,使用Future<映射<字符串,动态>gt;或者你要返回的任何东西(这将在未来对你有所帮助(:D

还有一点,你不需要在"initState(("中调用你的方法,因为FutureBuilder会为你做这件事:(

最新更新