颤振:空值错误时使用的空检查操作符



我正在制作一个简单的电子商务应用程序。我得到了如下错误:

E/flutter (13586): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)]未处理异常: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();
},
),
),
],
),
],
),
),
);
}
...

任何帮助都将不胜感激:/

编辑:

堆栈跟踪:

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

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

相关内容

  • 没有找到相关文章

最新更新