首页 > 解决方案 > 由于通知错误,无法访问 api 数据

问题描述

抱歉无法显示准确的 json 文件内容和 API URL 链接

这是我的 json 文件内容

<b>Notice</b>: Undefined index: api_token in <b>/home/websitename/public_html/catalog/controller/startup/session.php</b> on line <b>8</b>{
    "products": [
        {
            "product_id": "480",
            "thumb": "https://exmaple.com/image/cache/catalog/401/a32-1000x1000-270x270.png",
            "name": "Product Name",
            "description": "Discription",
            "special": false,
            "tax": false,
            "minimum": "1",
            "rating": 0,
            "href": "http://example.com/index.php?route=product/product&amp;product_id=480"
 
        },
]
}

这是我未来的功能

Future<ProductModel> getProducts() async {
  final String apiUrl = "https://www.example.com";
  final http.Response response = await http.get(apiUrl);
  var productModel;
  try {
    if (response.statusCode == 200) {
      var jsonString = response.body;
      var jsonMap = json.decode(jsonString);

      productModel = ProductModel.fromJson(jsonMap);
    }
  } catch (Exception) {
    print(Exception);
  }
  return productModel;
}

这是我们从中获取数据的 API 文件

<?php
class ControllerApiProduct extends Controller
{
    public function index()
    {
        $this->load->language('api/cart');
        $this->load->model('catalog/product');
        $this->load->model('tool/image');
        $json = array();
        $json['products'] = array();
        $filter_data = array();
        $results = $this->model_catalog_product->getProducts($filter_data);
        foreach ($results as $result) {
            if ($result['image']) {
                $image = $this->model_tool_image->resize($result['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_height'));
            } else {
                $image = $this->model_tool_image->resize('placeholder.png', $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_product_height'));
            }
            if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
                $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
            } else {
                $price = false;
            }
            if ((float) $result['special']) {
                $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
            } else {
                $special = false;
            }
            if ($this->config->get('config_tax')) {
                $tax = $this->currency->format((float) $result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']);
            } else {
                $tax = false;
            }
            if ($this->config->get('config_review_status')) {
                $rating = (int) $result['rating'];
            } else {
                $rating = false;
            }
            $data['products'][] = array(
                'product_id' => $result['product_id'],
                'thumb' => $image,
                'name' => $result['name'],
                'description' => utf8_substr(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('theme_' . $this->config->get('config_theme') . '_product_description_length')) . '..',
                'price' => $price,
                'special' => $special,
                'tax' => $tax,
                'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
                'rating' => $result['rating'],
                'href' => $this->url->link('product/product', 'product_id=' . $result['product_id']),
            );
        }
        $json['products'] = $data['products'];
        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }
}

这是我的未来建设者代码

FutureBuilder<ProductModel>(
        future: _productModel,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            print(snapshot.data.products.length);
            return GridView.builder(
              itemCount: snapshot.data.products.length,
              gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2),
              itemBuilder: (context, index) {
                return SingleProduct(
                  prodName: snapshot.data.products[index].name,
                  prodPicture: snapshot.data.products[index].thumb,
                  // prodOldPrice: productList[index]['oldPrice'],
                  prodPrice: snapshot.data.products[index].price,
                  prodDescription: snapshot.data.products[index].description,
                  prodId: snapshot.data.products[index].productId,
                );
              },
            );
          } else {
            // return CircularProgressIndicator();
            return CircularProgressIndicator();
          }
        }); 

我的代码正在运行,但由于此通知而无法获取 API 数据,<b>Notice</b>: Undefined index: api_token in <b>/home/example/public_html/catalog/controller/startup/session.php</b> on line <b>8</b>是否有解决此问题的解决方法。

确切的错误如下:

I/flutter (12559): FormatException: Unexpected character (at character 1)
I/flutter (12559): <b>Notice</b>: Undefined index: api_token in <b>/home/example/public_htm...
I/flutter (12559): ^

标签: phprestflutteropencart-3flutter-futurebuilder

解决方案


推荐阅读