更新的数据未显示在 Flutter Web App 上

分享于2022年07月17日 dart flutter php 问答
【问题标题】:更新的数据未显示在 Flutter Web App 上(Updated data not showing on Flutter Web App)
【发布时间】:2022-06-21 15:08:29
【问题描述】:

在我的颤振应用程序中,我有两个页面来显示和添加/编辑一些数据。我正在使用提供者和消费者构造器,但是当我添加或编辑数据并导航回显示我的数据的页面时,它们没有被更新。

Provider 函数正在运行以获取数据,但我猜 php 端有一些问题。 当 get 请求首先到达 php 文件时,它会检查密钥。

if (array_key_exists("parentid", $_GET)&&array_key_exists("moduleid", $_GET)) 

然后我像这样分开请求方法:

if($_SERVER['REQUEST_METHOD'] === 'GET') {..}
if($_SERVER['REQUEST_METHOD'] === 'DELETE') {..}

这是完整的代码:

if (array_key_exists("parentid", $_GET)&&array_key_exists("moduleid", $_GET)) {

error_log("This condition work fine");
$parentid = $_GET['parentid'];
$moduleid = $_GET['moduleid'];


if($parentid == '' || !is_numeric($parentid)) {
$response = new Response();
$response->setHttpStatusCode(400);
$response->setSuccess(false);
$response->addMessage("Parent ID cannot be blanck or must be numeric");
$response->send();
exit;
}
if($moduleid == '' || !is_numeric($moduleid)) {
$response = new Response();
$response->setHttpStatusCode(400);
$response->setSuccess(false);
$response->addMessage("Module ID cannot be blanck or must be numeric");
$response->send();
exit;
}


if($_GET) {
    error_log("here we stack");
    try { 
       
       $querySetNames = $readDB->prepare("SET NAMES 'utf8'");
       $querySetNames->execute();
       

        $query = $readDB->prepare('SELECT ... }

if($_SERVER['REQUEST_METHOD'] === 'DELETE') {..}
if($_SERVER['REQUEST_METHOD'] === 'PATCH') {..}

}

我尝试使用 if($_SERVER['REQUEST_METHOD'] == 'GET') {..} if($_SERVER['REQUEST_METHOD'] === 'GET') {..} 而不是 if($_GET) 。但它没有用。

因此,我无法获得更新的数据。可能是什么原因?


【解决方案1】:

这是解决方案,我在这个网站上找不到真正的答案,但如果您遇到同样的问题,您需要在 API URL 的末尾添加随机数。例如,如果您的 API URL 是:

final response = await http
  .get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));

一定是:

import 'dart:math';
Random random = new Random();
int randomNumber = random.nextInt(100);

final response = await http
      .get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1?srn='+randomNumber ));