Flutter: null check operator used on a null value error

1.2k Views Asked by At

I'm working on a simple e-commerce app. And I got an error as follows:

E/flutter (13586): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value

Can't figure what's causing this error even though I saw similar cases and tried to handle null values in my code:

all_products.dart:

...
Widget AllProducts() {
  ...
      WatchBoxBuilder(
  box: Hive.box<Product>('products'),
  builder: (context, box) {
    if (box != null) {
      return ListView.builder(
        padding: EdgeInsets.only(
            top: 10, left: 25, right: 25),
        itemCount: products.length,
        itemBuilder: (BuildContext context, int idx) {
          return Padding(
            padding: EdgeInsets.only(bottom: 10),
            child: buildProduct(idx),
          );
        },
      );
    } else return Container();
  },
  ),
  ],
  );
}
...
Widget buildProduct(int idx) {
  return Container(
    height: 250,
    decoration: BoxDecoration(
      color: Colors.deepPurple[600],
      borderRadius: BorderRadius.all(Radius.circular(30)),
      boxShadow: [BoxShadow(color: Colors.deepPurple, blurRadius: 7)],
    ),
  child: Row(
    mainAxisAlignment: MainAxisAlignment.start,
      textDirection: TextDirection.ltr,
      children: [
        Expanded(
  child: Container(
    padding: EdgeInsets.only(left: 10),
    child: Image.file(File(product(idx).image), width: 150, height: 150),
    alignment: Alignment.centerLeft,
        ),
        ),
    Expanded(
      child: Column(
children: [
Padding(
  padding: const EdgeInsets.only(top: 15, right: 10),
  child:   Container(
    height: 30,
    child:   Text(
      product(idx).name,
      style: GoogleFonts.lato(fontSize: 17.5, fontWeight: FontWeight.bold),
      textAlign: TextAlign.center,
    ),
  ),
),
    Padding(
      padding: const EdgeInsets.only(top: 10),
      child: Text(
  product(idx).description,
  style: GoogleFonts.lato(fontSize: 12.5, color: Colors.white),
        textAlign: TextAlign.left,
  ),
    ),
],
          ),
    ),
      ],
    ),
  );
}
...

product_details.dart:

...
final picker = ImagePicker();
final titleCtrl = TextEditingController();
final descriptionCtrl = TextEditingController();
final priceCtrl = TextEditingController();
String? image;

Widget ProductDetails() {
  ...
 TextField(
              decoration: InputDecoration(
                icon: Icon(Icons.title),
                labelText: 'Product\'s name',
              ),
              controller: titleCtrl,
            ),
          ),
      Padding(
        padding: const EdgeInsets.only(bottom: 15),
        child: TextField(
          decoration: InputDecoration(
            icon: Icon(Icons.article_outlined),
            labelText: 'Product\'s specifications',
            hintText: 'Describe the product...'
          ),
          maxLines: null,
          controller: descriptionCtrl,
        ),
          ),
    Padding(
      padding: const EdgeInsets.only(bottom: 15),
      child: TextField(
        decoration: InputDecoration(
          icon: Icon(Icons.attach_money),
          labelText: 'Product\'s price',
        ),
        keyboardType: TextInputType.number,
        controller: priceCtrl,
      ),
    ),
          Padding(
            padding: const EdgeInsets.only(bottom: 10),
            child: Container(
              height: 100,
              decoration: BoxDecoration(
                color: Colors.deepPurple[300],
                borderRadius: BorderRadius.all(Radius.circular(10)),
              ),
              child: image != null ? Image.file(File(image!)) :
              Center(child: Icon(Icons.image_outlined, size: 30)),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(bottom: 15),
            child: OutlinedButton(
               child: Text('Pick an image from gallery'),
              onPressed: () async {
                 image = await picker.pickImage(
                     source: ImageSource.gallery).toString();
              },
            ),
          ),
          ...
OutlinedButton(
                  child: Text('Save'),
                  onPressed: () {
                    Product product = Product(
                      name: titleCtrl.text != null ? titleCtrl.text : '',
                      description: descriptionCtrl.text != null ?
                      descriptionCtrl.text : '',
                      price: priceCtrl.text != null ? priceCtrl.text : '',
                      image: image != null ? image! : '',
                    );
                    addProduct(product);
                    Get.back();
                   },
  ),
  ),
            ],
          ),
        ],
      ),
    ),
  );
}
...

Any help will be appreciated :/

Edit:

stack trace:

E/flutter ( 3030): #0      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:121:86)
E/flutter ( 3030): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:146:36)
E/flutter ( 3030): #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
E/flutter ( 3030): #3      MethodChannelPathProvider.getApplicationDocumentsPath (package:path_provider_platform_interface/src/method_channel_path_provider.dart:52:10)
E/flutter ( 3030): #4      getApplicationDocumentsDirectory (package:path_provider/path_provider.dart:115:40)
E/flutter ( 3030): #5      main (package:apple_estore/main.dart:8:30)
E/flutter ( 3030): #6      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:145:25)
E/flutter ( 3030): #7      _rootRun (dart:async/zone.dart:1428:13)
E/flutter ( 3030): #8      _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 3030): #9      _runZoned (dart:async/zone.dart:1863:10)
E/flutter ( 3030): #10     runZonedGuarded (dart:async/zone.dart:1851:12)
E/flutter ( 3030): #11     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:141:5)
E/flutter ( 3030): #12     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
E/flutter ( 3030): #13     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter ( 3030):
1

There are 1 best solutions below

2
On

Problem in buildProduct(idx). ListViewBuilder could not get all the values from db (some are null or empty may be). so comment one by one list items and check result. for more please provide buildProduct() code.