我正在構建一個簡單的電子商務應用程式,但是當我的未來功能無限重新加載時,我陷入了困境:
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”的值。
我的初始化函式:
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。
uj5u.com熱心網友回復:
您需要從您的“getCartData()”方法回傳一個 Future,現在您只回傳一個 Hashmap 而不是 futureBuilder 需要的 Future。你可以這樣做:
return Future.value(data);
還有一件事是您必須只傳遞對方法的參考而不是呼叫它,如下所示:
future: getCartData,
還請指定回傳型別,使用 Future<Map<String, dynamic>> 或您回傳的任何內容(這將對您將來有所幫助):D
還有一個,你不需要在“initState()”中呼叫你的方法,因為 FutureBuilder 會為你做這件事:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/421011.html
標籤:
