簡體   English   中英

增加flutter中api的響應

[英]Increment the response from the api in flutter

在此處輸入圖像描述

這里我從 API 獲取數據以在第一張卡中“投票”

我試圖通過按遞增和遞減按鈕來增加后端的“投票”值數據(不影響 Api 響應)(后端我們需要通過采用原始值來增加“投票”的值)

我試圖將“投票”數據存儲在一個變量中並在后端增加投票值然后打印后端增加/減少值

這是代碼

              return Card(
                child: Column(
                  children: [
                    Row(
                      children: [
                        Container(
                          height: 140,
                          width: 70,
                          child: Card(
                            semanticContainer: true,
                            clipBehavior: Clip.antiAliasWithSaveLayer,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(10.0),
                            ),
                            elevation: 5,
                            margin: EdgeInsets.all(5),
                            child: Column(
                              children: [
                                IconButton(
                                    onPressed: () {},
                                    icon: const Icon(Icons.arrow_drop_up,
                                        size: 50)),
                                Text(result![index].voting.toString()),
                                IconButton(
                                    onPressed: () {},
                                    icon: const Icon(Icons.arrow_drop_down,
                                        size: 45)),
                                const Text("Votes"),
                              ],
                            ),
                          ),
                        ),
                        Container(
                          height: 140,
                          width: 80,
                          child: Card(
                            semanticContainer: true,
                            clipBehavior: Clip.antiAliasWithSaveLayer,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(5.0),
                            ),
                            elevation: 5,
                            margin: const EdgeInsets.all(5),
                            child: Image.network(
                                result[index].poster.toString(),
                                fit: BoxFit.fill),
                          ),
                        ),
                        Expanded(
                            child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(result![index].title.toString(),
                                style: const TextStyle(fontSize: 25)),
                            const SizedBox(
                              height: 10,
                            ),
                            Text(
                                'Genre:${result![index].genre.toString()}'),
                            Text(
                                'Director:${result![index].director.first.toString()}'),
                            Text(
                                'Writter :${result![index].writers.first.toString()}'),
                            Row(
                              children: [
                                Text(result![index].runTime.toString()),
                                const Text("|"),
                                Text(result![index].language.toString()),
                                const Text("|"),
                                Text(result[index].releasedDate.toString()),
                              ],
                            ),
                            Row(
                              children: <Widget>[
                                Text(
                                    '${result![index].pageViews.toString()} Views'),
                                const Text("||"),
                                Text(
                                    'Voted By ${result[index].voting}people'),
                              ],
                            ),
                          ],
                        ))
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Padding(
                          padding: const EdgeInsets.all(10.0),
                          child: SizedBox(
                              height: 35,
                              width: 330,
                              child: ElevatedButton(
                                onPressed: () {},
                                style: TextButton.styleFrom(
                                  primary: Colors.white,
                                  backgroundColor: Colors.blue,
                                  // minimumSize: const Size.fromHeight(20),// Background Color
                                ),
                                child: const Text("Watch Trailor",
                                    style: TextStyle(fontSize: 15)),
                              )),
                        ),
                      ],
                    ),
                  ],
                ),
              );
            },
          ),
        ),
 
);

} }

當你像 bool 標志一樣發送請求時,我會添加某種鎖定機制,例如這里有一些代碼可以更好地解釋它

bool loading = false;
method to call when incrementing
   incrment() async{
   loading = true;
   setstate((){});
   await API_CALL; //handle increment based on response
   loading = false;
   }

現在只需在 onpress 上添加此方法,這樣您就可以維護准確的數據;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM