0

Sorry can't able to show exact json file contents and API URL links

this is my json file contents

<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"
 
        },
]
}

this is my future function

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;
}

This is the API file from which we are getting data

<?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));
    }
}

here is my code of future builder

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();
          }
        }); 

My code is working but it is not able to get API data due to this notice <b>Notice</b>: Undefined index: api_token in <b>/home/example/public_html/catalog/controller/startup/session.php</b> on line <b>8</b> is there a workaround to solve this.

the exact error is given below:

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): ^
  • Error state that there is error in API that is Undefined index: api_token , make sure you are passing the proper URL and parameters. – vikas Feb 11 '21 at 07:12
  • @vikas Actually, we are developing our product by using the open-cart default APIs to access those APIs we need an API access token which is not generating at this moment we try the workaround and created an API file product.php (i have just added the code now in the post) which didn't require any access token you just need the API URL to work with this – Ketan aggarwal Feb 11 '21 at 07:53
  • Try hitting the API in browser as it does not have parameters, the issue is not at flutter side. Or You can hit the API in Postman. You will see API throwing an error. – vikas Feb 11 '21 at 09:34
  • @vikas I try that part too and getting the same notice and data my actual problem is that the flutter is not the fetching data due to this notice, but when I hit that same API in the postman or in my browser I am getting notice + data as you can see in my post at the top, I think the problem might be related with the cURL because the file product.php is on cURL – Ketan aggarwal Feb 11 '21 at 10:21

0 Answers0