请帮助我修复以下代码中的错误:
import 'dart:io';
import 'package:dotted_border/dotted_border.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:typof_app/controllers/add_product_controller.dart';
import 'package:typof_app/controllers/category_controllers.dart';
import 'package:typof_app/controllers/product_controllers.dart';
import 'package:typof_app/data/api/images_api.dart';
import 'package:typof_app/routes/routes.dart';
import 'package:typof_app/ui/widget/big_text.dart';
import 'package:typof_app/ui/widget/custom_loader.dart';
import 'package:typof_app/ui/widget/small_text.dart';
import 'package:typof_app/ui/widget/text_form.dart';
import 'package:typof_app/ui/widget/show_custom_snackbar.dart';
import 'package:typof_app/ui/utiles/colors.dart';
import 'package:typof_app/ui/utiles/dimensions.dart';
class UploadProduct extends StatefulWidget {
const UploadProduct({super.key});
@override
State<UploadProduct> createState() => _UploadProductState();
}
class _UploadProductState extends State<UploadProduct> {
var skuAutoGen = '1';
var _selectedStatus = 'D';
var _selectedCategory = '';
var _selectedSubCategory = '';
var _selectedSaleable = '';
List<String> categoryList = [];
List<dynamic> subCategoryList = [];
List<String> statusList = [
"P",
"D",
];
List<String> saleableList = [
"yes",
"no",
];
File? _pickedFile;
CroppedFile? _croppedFile;
final List prodctImageList = [];
final picker = ImagePicker();
List catlist = Get.find<CategoryControllers>().categoryList;
//String skuAutoGenerated ='add';
onSelectCategory(String cat) {
for (int i = 0; i < catlist.length; i++) {
if (catlist[i].categoryName == cat) {
if (catlist[i].subCategory.length > 0) {
subCategoryList = catlist[i].subCategory;
} else {
subCategoryList = [];
}
}
}
}
_uploadImgGallery() async {
final PickedFile = await picker.pickImage(source: ImageSource.gallery);
if (PickedFile != null) {
CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: PickedFile.path,
// aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
aspectRatioPresets: [
CropAspectRatioPreset.square,
// CropAspectRatioPreset.ratio2x3,
// CropAspectRatioPreset.ratio3x2,
// CropAspectRatioPreset.original,
// CropAspectRatioPreset.ratio4x3,
// CropAspectRatioPreset.ratio16x9
],
uiSettings: [
AndroidUiSettings(
toolbarTitle: 'Edited',
toolbarColor: appColor.backgroundColor,
toolbarWidgetColor: Colors.white,
initAspectRatio: CropAspectRatioPreset.square,
lockAspectRatio: false),
IOSUiSettings(
title: 'Cropper',
),
WebUiSettings(
context: context,
),
],
);
if (croppedFile != null) {
setState(() {
_croppedFile = croppedFile;
_pickedFile = File(croppedFile.path);
Get.back();
// prodctImageList.add(File(croppedFile.path));
});
}
}
}
Future _uploadImgCamera() async {
final PickedFile = await picker.pickImage(
//source: source,
source: ImageSource.camera);
if (PickedFile != null) {
CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: PickedFile.path,
// aspectRatio: CropAspectRatio(ratioX: 2, ratioY: 3),
aspectRatioPresets: [
CropAspectRatioPreset.square,
],
uiSettings: [
AndroidUiSettings(
backgroundColor: Colors.red,
toolbarTitle: 'Edited',
toolbarColor: appColor.backgroundColor,
toolbarWidgetColor: Colors.white,
initAspectRatio: CropAspectRatioPreset.square,
lockAspectRatio: false),
IOSUiSettings(
title: 'Cropper',
),
WebUiSettings(
context: context,
),
],
);
if (croppedFile != null) {
setState(() {
_croppedFile = croppedFile;
_pickedFile = File(croppedFile.path);
Get.back();
//prodctImageList.add(croppedFile.path);
});
}
}
}
TextEditingController productNameController = TextEditingController();
TextEditingController categoryController = TextEditingController();
TextEditingController sellingPriceController = TextEditingController();
TextEditingController mrpController = TextEditingController();
TextEditingController gstController = TextEditingController();
TextEditingController costController = TextEditingController();
TextEditingController shippingCostController = TextEditingController();
//TextEditingController hsnController = TextEditingController();
TextEditingController availableForSaleController = TextEditingController();
TextEditingController skuController = TextEditingController();
TextEditingController statusController = TextEditingController();
String sku = '';
String productF2L = '';
String categoryF2L = '';
String productName = '';
String categoryName = '';
@override
void initState() {
// TODO: implement initState
for (int i = 0; i < catlist.length; i++) {
categoryList.add(catlist[i].categoryName.toString());
}
}
skuAutoGenerated() {
String productF2L = productName.toString().substring(0, 2).toUpperCase();
print(productF2L);
String categoryF2L = _selectedCategory.substring(0, 2).toUpperCase();
print(categoryF2L);
var list = Get.find<ProductControllers>();
var all = list.allProductList.length + 1;
String sku = "$productF2L-$categoryF2L-$all";
skuAutoGen = sku;
print(sku);
print(skuController);
}
void _uploadProduct(context) async {
File? pickedFile = _pickedFile;
final String productName = productNameController.text.trim();
final String category = _selectedCategory;
final String subCategory = _selectedSubCategory;
final String price = sellingPriceController.text;
final String mrp = mrpController.text != '' ? mrpController.text : '0';
final String gst = gstController.text != '' ? gstController.text : '0';
final String cost = costController.text != '' ? costController.text : '0';
final String shippingCost =
shippingCostController.text != '' ? shippingCostController.text : '0';
final String availableForSale = availableForSaleController.text;
final String sku = skuController.text.trim().isNotEmpty
? skuController.text.trim()
: skuAutoGen;
//final String hsn = hsnController.text.trim();
final String status = _selectedStatus;
final saleable = _selectedSaleable;
final productId = '';
print(sku);
int pri = int.parse(price);
int marketPrices = int.parse(mrp);
if (pickedFile == null) {
showCustomSnackBar("Upload Product Images", title: 'Product Images');
} else if (productName.isEmpty) {
showCustomSnackBar("Enter product name", title: 'Product Name');
} else if (category.isEmpty) {
showCustomSnackBar("Select category", title: 'Category');
} else if (subCategoryList.isNotEmpty && subCategory.isEmpty) {
showCustomSnackBar("Select SubCategory", title: 'Category');
} else if (price.isEmpty) {
showCustomSnackBar("Enter product selling price", title: 'Selling Price');
} else if (pri > marketPrices) {
showCustomSnackBar("Enter product selling price", title: 'MRP Price');
} else if (availableForSale.isEmpty) {
showCustomSnackBar("Enter product available quandity",
title: 'Available For Sale');
} else if (sku.isEmpty) {
showCustomSnackBar("Enter product sku", title: 'SKU');
} else if (status.isEmpty) {
showCustomSnackBar("Select product status", title: 'Status');
} else if (saleable.isEmpty) {
showCustomSnackBar("Select product saleable", title: 'Saleable');
} else {
await Get.find<AddProductControllers>()
.addProduct(
productName,
category,
subCategory,
price,
mrp,
gst,
cost,
shippingCost,
availableForSale,
sku,
status,
saleable,
)
.then((value) async {
if (value.status == true) {
ProductImageApi productImageApi = ProductImageApi();
await productImageApi.UploadImage(value.pid, pickedFile)
.then((value) {
if (value.status == true) {
showCustomSnackBarSuccess("Product uploaded Successfully",
title: 'Perfect');
int index = 3;
Get.find<ProductControllers>().getProductList();
Get.offAllNamed(Routes.getbuttonNavigationIndex(index));
} else {
showCustomSnackBarError("Product uploaded fail", title: 'Errors');
}
});
} else if (value.status == false) {
showCustomSnackBarError(value.error.toString(),
title: value.status.toString());
}
});
}
}
var addCategory = Get.find<CategoryControllers>();
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
backgroundColor: appColor.appbarColor,
leading: IconButton(
icon: const Icon(Icons.arrow_back),
color: Colors.white,
onPressed: () {
Get.back();
},
),
title: BigText(
text: 'Add Products',
color: Colors.white,
),
actions: [
Padding(
padding: const EdgeInsets.all(10),
child: GestureDetector(
onTap: () {
_uploadProduct(context);
},
child: Container(
//height: 20,
width: 65,
decoration: BoxDecoration(
color: const Color.fromARGB(255, 202, 26, 85),
//border: Border.all(color: Colors.blueAccent),
borderRadius: BorderRadius.circular(Dimensions.radius10),
),
child: const Center(child: Text('Save')),
),
),
)
],
),
body: GetBuilder<AddProductControllers>(
builder: (addProductControllers) {
return
// !addProductControllers.isLoaded
const Text('data');
},
),
),
);
}
Widget addProductImages() {
return Container(
child: _pickedFile != null
? Image.file(
_pickedFile!,
//fit: BoxFit,
)
: TextButton(
onPressed: () {
bottomSheetPage(context);
print('upload');
},
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SmallText(
text: "Product Image",
size: Dimensions.font20,
color: Colors.white,
),
SmallText(
text: "*",
size: Dimensions.font20,
color: Colors.red,
),
],
),
SmallText(
text: "Only one Image can be uploaded at a time"),
],
),
)));
}
void bottomSheetPage(context) {
showModalBottomSheet(
// backgroundColor: Colors.amber,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(25),
),
),
context: context,
builder: (context) {
return Container(
decoration: const BoxDecoration(
color: appColor.backgroundColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(25),
topRight: Radius.circular(25))),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const SizedBox(
height: 20,
),
Container(
height: 5,
width: 45,
decoration: const BoxDecoration(
color: Color.fromARGB(255, 128, 128, 128),
borderRadius: BorderRadius.all(Radius.circular(10))),
),
const SizedBox(
height: 20,
),
Padding(
padding: const EdgeInsets.all(25),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(
children: [
Container(
height: 70,
width: 70,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
color: Colors.black45,
borderRadius: BorderRadius.circular(50)),
child: IconButton(
icon: const Icon(Icons.camera_alt_outlined),
tooltip: 'Upload Photos for Camera',
onPressed: () {
setState(() {
_uploadImgCamera();
print('Camera open');
});
}),
// Center(child: Icon(Icons.camera_alt_outlined)),
),
const SizedBox(
height: 10,
),
const Text('Camera')
],
),
const SizedBox(
width: 55,
),
Column(
children: [
Container(
height: 70,
width: 70,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
color: Colors.black45,
borderRadius: BorderRadius.circular(50)),
child: IconButton(
icon: const Icon(
Icons.photo_camera_back_outlined),
tooltip: 'Upload Photos for Gallery',
onPressed: () {
setState(() {
_uploadImgGallery();
print('Gallery open');
});
})),
const SizedBox(
height: 10,
),
const Text('Gallery')
],
),
],
),
)
],
),
);
});
}
}
错误是:
在构建KeyedSubtree-[GlobalKey#f5a53]时抛出了以下_CastError:用于空值的空检查运算符
导致错误的相关小部件是Scaffold upload_product_screen.dart:276当抛出异常时,这是堆栈
我想要解决方案。
1条答案
按热度按时间zzoitvuj1#
您已初始化文件?_pickedFile;它可能是空的。试着传递它是这样的吗