Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The method '[]' was called on null #10

Closed
sidrahassanpk opened this issue Dec 1, 2019 · 0 comments
Closed

The method '[]' was called on null #10

sidrahassanpk opened this issue Dec 1, 2019 · 0 comments

Comments

@sidrahassanpk
Copy link

sidrahassanpk commented Dec 1, 2019

Getting following error:-

The method '[]' was called on null.
Receiver: null
Tried calling: []("images")

When the exception was thrown, this was the stack
#0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:51:5)
#1      _MyOrderPageState.build.<anonymous closure>.<anonymous closure> 
package:apps/order.dart:80
#2      SliverChildBuilderDelegate.build 
package:flutter/…/widgets/sliver.dart:442
#3      SliverMultiBoxAdaptorElement._build.<anonymous closure> 
package:flutter/…/widgets/sliver.dart:1153
#4      _HashMap.putIfAbsent  (dart:collection-patch/collection_patch.dart:137:29)

Here is the code:-

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:woocommerce_api/woocommerce_api.dart';


class OrderPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyOrderPage(title: 'WooCommerce API Demo'),
    );
  }
}

class MyOrderPage extends StatefulWidget {
  MyOrderPage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyOrderPageState createState() => _MyOrderPageState();
}

class _MyOrderPageState extends State<MyOrderPage> {

  List<Widget> products = [];

  Future getProducts() async {

    /// Initialize the API
    WooCommerceAPI wcApi = WooCommerceAPI(
        "https://panahgah.com",
        "*****************************************************",
        "*****************************************************"
    );
    
    /// Get data using the endpoint
    var p = await wcApi.getAsync("products");
    return p;
  }


  @override
  void initState() {
    super.initState();
  }
  

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      
      body: FutureBuilder(
        future: getProducts(),
        builder: (_, s){

          if(s.data == null){
            return Container(
              child: Center(
                child: Text("Loading..."),
              ),
            );
          }

          return ListView.builder(
            itemCount: s.data.length,
            itemBuilder: (_, index){
            
            /// create a list of products
              return ListTile(
                leading: CircleAvatar(
                  child: Image.network(s.data[index]["images"][0]["src"]),
                ), 
                title: Text(s.data[index]["name"]),
                subtitle: Text("Buy now for \$ " + s.data[index]["price"]),
              );

            }
          );
        },
      ),
    );
  }
}

My JSON is working fine. Here is the output from my website:-

[
{
"id": 24,
"name": "Ginger",
"slug": "ginger",
"permalink": "https://panahgah.com/product/ginger/",
"date_created": "2019-11-30T17:57:51",
"date_created_gmt": "2019-11-30T17:57:51",
"date_modified": "2019-11-30T17:57:55",
"date_modified_gmt": "2019-11-30T17:57:55",
"type": "simple",
"status": "publish",
"featured": false,
"catalog_visibility": "visible",
"description": "",
"short_description": "",
"sku": "",
"price": "30",
"regular_price": "30",
"sale_price": "",
"date_on_sale_from": null,
"date_on_sale_from_gmt": null,
"date_on_sale_to": null,
"date_on_sale_to_gmt": null,
"price_html": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#8360;</span>30.00</span>",
"on_sale": false,
"purchasable": true,
"total_sales": 4,
"virtual": false,
"downloadable": false,
"downloads": [],
"download_limit": -1,
"download_expiry": -1,
"external_url": "",
"button_text": "",
"tax_status": "taxable",
"tax_class": "",
"manage_stock": false,
"stock_quantity": null,
"stock_status": "instock",
"backorders": "no",
"backorders_allowed": false,
"backordered": false,
"sold_individually": false,
"weight": "",
"dimensions": {
"length": "",
"width": "",
"height": ""
},
"shipping_required": true,
"shipping_taxable": true,
"shipping_class": "",
"shipping_class_id": 0,
"reviews_allowed": false,
"average_rating": "0.00",
"rating_count": 0,
"related_ids": [
22,
21
],
"upsell_ids": [],
"cross_sell_ids": [],
"parent_id": 0,
"purchase_note": "",
"categories": [
{
"id": 22,
"name": "Vegetables",
"slug": "vegetables"
}
],
"tags": [],
"images": [],
"attributes": [
{
"id": 1,
"name": "Unit",
"position": 0,
"visible": true,
"variation": false,
"options": [
"1/4 KG"
]
}
],
"default_attributes": [],
"variations": [],
"grouped_products": [],
"menu_order": 0,
"meta_data": [],
"_links": {
"self": [
{
"href": "https://panahgah.com/wp-json/wc/v3/products/24"
}
],
"collection": [
{
"href": "https://panahgah.com/wp-json/wc/v3/products"
}
]
}
},
]

P.S: I have hidden Consumer Key and Consumer Secret for this post

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant