颤音:空检查操作员在零值错误上使用

发布于 2025-01-22 03:23:12 字数 6249 浏览 0 评论 0原文

我正在研究一个简单的电子商务应用程序。我有以下错误的错误:

e/flutter(13586):[错误:flutter/lib/ui/ui_dart_state.cc(209)]未手持的例外:null check Operator在Null值

can 上使用的null检查操作员't算出什么是什么原因导致了此错误,即使我看到了类似的情况并尝试在我的代码中处理null值:

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

任何帮助:/

编辑:

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):

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):

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2025-01-29 03:23:12

BuildProduct(IDX)中的问题。 ListViewBuilder无法从数据库中获取所有值(有些是空或空的)。因此,请一一评论列表项目并检查结果。有关更多信息,请提供BuildProduct()代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文