在我的电子商务应用程序中,我有ProductsScreen
一个按钮,用于推动NewEditProductScreen
创建新产品表或编辑现有产品表。
我使用 ImagePicker 和 ImagePickerWeb 来选择产品表的图片,并且因为我需要一个通用格式用于 Web 和设备应用程序版本,所以我使用 Uint8List 来选择图片。然后将其上传到返回 downloadUrl 的 Firebase Storage(所有工作)。然后将产品保存到 sembast(用于本地库存)和 firebase 以进行电子商务产品搜索。
为了保存选择到 sembast 的 Uint8List,我在jsonEncode
模型的方法中使用。现在的问题是,在按预期显示拾取的图像时,
在加载产品时,显示的图像没有显示。与编码之前一样,图像按预期显示,但在编码/解码之后,我猜不出来并且不能很好地与 Uint8List 一起工作?我还能尝试将 Uint8List 保存到 sembast 吗?我试图不将其解码/编码为 sembast,但它没有帮助。toMap()
jsonDecode
fromMap()
NewEditProductScreen
Image.memory(imageData)
ProductScreen
Image.memory(productImage)
jsonEncode
jsonDecode
NewEditProductScreen 接收拾取的图像状态:
if (state is PickedImage) {
setState(() {
imageData = state.imageData;
print(' ###### Uint8List picked image is: ${state.imageData}');
});
}
NewEditProductScreen 显示它:
Container(
color: Colors.lightBlueAccent,
child: Stack(children: [
Center(
child: Image.memory(imageData), //productImage),
),
GestureDetector(
onTap: () {
BlocProvider.of<ProductBloc>(context)
.add(PickImage());
},
child: Center(
child: AutoSizeText(
AppLocalizations.instance.text('Choose image'),
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 15,
fontWeight: FontWeight.w500),
minFontSize: 8,
maxLines: 1,
group: autoSizeGroup,
),
),
),
]),
),
ProductsScreen 加载选定的产品状态:
setState(() {
productId = state.product.productId;
productName = state.product.productName;
brand = state.product.brand;
price = state.product.price;
productDescription = state.product.productDescription;
category = state.product.category;
isPromotion = state.product.isPromotion;
vendor = state.product.vendor;
barcode = state.product.barcode;
productImage = state.product.productImage;
imageUrl = state.product.imageUrl;
minimumStock = state.product.minimumStock;
availableQuantity = state.product.availableQuantity;
soldQuantity = state.product.soldQuantity;
print(
' ###### Uint8List image is: ${state.product.productImage}');
});
}
和图像显示:
Container(
color: Colors.lightBlueAccent,
child: Center(
child: Image.memory(
productImage), //productImage),
),
),
这是产品型号:
class Product {
final String productId;
final String productName;
final String brand;
final String price;
final String productDescription;
final String category;
final bool isPromotion;
final String vendor;
final String barcode;
String imageUrl;
final Uint8List productImage;
int minimumStock;
int availableQuantity;
int soldQuantity;
Product(
{@required this.productId,
@required this.productName,
@required this.brand,
@required this.price,
@required this.productDescription,
@required this.category,
@required this.isPromotion,
@required this.vendor,
@required this.barcode,
@required this.imageUrl,
@required this.productImage,
@required this.minimumStock,
@required this.availableQuantity,
@required this.soldQuantity});
Map<String, dynamic> toMap() {
return {
'productId': productId,
'productName': productName,
'brand': brand,
'price': price,
'productDescription': productDescription,
'category': category,
'isPromotion': isPromotion,
'vendor': vendor,
'barcode': barcode,
'imageUrl': imageUrl,
'productImage': jsonEncode(productImage),
'minimumStock': minimumStock,
'availableQuantity': availableQuantity,
'soldQuantity': soldQuantity,
};
}
static Product fromMap(Map<String, dynamic> map) {
return Product(
productId: map['productId'],
productName: map['productName'],
brand: map['brand'],
price: map['price'],
productDescription: map['productDescription'],
category: map['category'],
isPromotion: map['isPromotion'],
vendor: map['vendor'],
barcode: map['barcode'],
imageUrl: map['imageUrl'],
productImage: jsonDecode(map['productImage']),
minimumStock: map['minimumStock'],
availableQuantity: map['availableQuantity'],
soldQuantity: map['soldQuantity'],
);
}
Map<String, dynamic> toFirebase() {
return {
'Product Id': productId,
'Product Name': productName,
'Brand': brand,
'Product Price': price,
'Product Description': productDescription,
'Product Category': category,
'isPromotion': isPromotion,
'Product Vendor': vendor,
'Code': barcode,
'Product Picture Url': imageUrl,
'Minimum Stock': minimumStock,
'Available Quantity': availableQuantity,
'SoldQuantity': soldQuantity,
};
}
static Product fromFirebase(Map<String, dynamic> map) {
return Product(
productId: map['Product Id'],
productName: map['Product Name'],
brand: map['Brand'],
price: map['Price'],
productDescription: map['Product Description'],
category: map['Product Category'],
isPromotion: map['isPromotion'],
vendor: map['Product Vendor'],
barcode: map['Code'],
imageUrl: map['Product Picture Url'],
minimumStock: map['Minimum Stock'],
availableQuantity: map['Available Quantity'],
soldQuantity: map['Sold Quantity'],
);
}
}