firebase 存储的图像 link 正在返回 null - Flutter
Image link of firebase storage is returning null - Flutter
在显示图像的 fiorebase 存储 link 时,出现此错误
network_image_web.dart:26:16
url != null 不正确
下面是示例。是的,除了 pic
之外,提供者正在处理所有其他字段
class AddAlreadyExistingProductsView extends StatefulWidget {
@override
_AddAlreadyExistingProductsViewState createState() =>
_AddAlreadyExistingProductsViewState();
}
class _AddAlreadyExistingProductsViewState
extends State<AddAlreadyExistingProductsView> {
@override
Widget build(BuildContext context) {
final _productsList = Provider.of<List<Product>>(context);
return Scaffold(
............
child:
SearchableDropdown.single(
............
items: _productsList.map((value) {
print("---> pic : " + value.pic.toString()); // <--- NULL HERE
..........
child: ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(value.pic),
),
subtitle: Text(value.name)
.............
)
)
}
class MainDataProvider extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
StreamProvider<List<Product>>.value(
value: DatabaseService().getAllProducts,
........
],
child: MaterialApp(
home: AddAlreadyExistingProductsView (),
),
);
}
}
我也试过打印,打印图片的时候返回null。检索所有其他字段都可以。我在网络图像中给出了存储在 firebase 数据库中的图像的静态值,它工作正常。但是在动态获取数据时它不起作用。我不知道为什么。我在移动设备上以相同的方式获取数据并且它有效但对于 flutter web 它不起作用。
这是产品型号代码
Stream<List<Product>> get getAllProducts {
return productCollection.get().asStream().map(_productDataFromSnapshot);
}
List<Product> _productDataFromSnapshot(QuerySnapshot snapshot) {
List<Product> productsList = List<Product>();
for (int i = 0; i < snapshot.docs.length; i++) {
productsList.add(Product(
productID: snapshot.docs[i].data()['productID'] ?? "",
name: snapshot.docs[i].data()['name'] ?? "",
volume: snapshot.docs[i].data()['volume'] ?? "",
price: snapshot.docs[i].data()['price'] ?? "",
quantity: snapshot.docs[i].data()['quantity'] ?? "",
brand: snapshot.docs[i].data()['brand'] ?? "",
productAttribute: snapshot.docs[i].data()['productAttribute'] ?? "",
productCtgName: snapshot.docs[i].data()['productCtgName'] ?? "",
productSubCtgName: snapshot.docs[i].data()['productSubCtgName'] ?? "",
productCtgID: snapshot.docs[i].data()['productCtgID'] ?? "",
productSubCtgID: snapshot.docs[i].data()['productSubCtgID'] ?? "",
));
}
return productsList;
}
class Product {
String productID;
String name;
int price;
int salePrice;
DateTime addDate;
String volume;
int quantity;
String productAttribute;
String productSubCtgID;
String productCtgID;
String productSubCtgName;
String productCtgName;
String brand;
String pic;
Product({
this.productID,
this.name,
this.price,
this.addDate,
this.volume,
this.quantity,
this.productAttribute,
this.brand,
this.productCtgID,
this.productSubCtgID,
this.productCtgName,
this.productSubCtgName,
this.pic,
this.salePrice,
});
在 for 循环的 _productDataFromSnapshot 函数中,您没有初始化 pic 字段。
为防止此类错误,请对此类字段使用@required
在显示图像的 fiorebase 存储 link 时,出现此错误 network_image_web.dart:26:16 url != null 不正确
下面是示例。是的,除了 pic
之外,提供者正在处理所有其他字段class AddAlreadyExistingProductsView extends StatefulWidget {
@override
_AddAlreadyExistingProductsViewState createState() =>
_AddAlreadyExistingProductsViewState();
}
class _AddAlreadyExistingProductsViewState
extends State<AddAlreadyExistingProductsView> {
@override
Widget build(BuildContext context) {
final _productsList = Provider.of<List<Product>>(context);
return Scaffold(
............
child:
SearchableDropdown.single(
............
items: _productsList.map((value) {
print("---> pic : " + value.pic.toString()); // <--- NULL HERE
..........
child: ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(value.pic),
),
subtitle: Text(value.name)
.............
)
)
}
class MainDataProvider extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
StreamProvider<List<Product>>.value(
value: DatabaseService().getAllProducts,
........
],
child: MaterialApp(
home: AddAlreadyExistingProductsView (),
),
);
}
}
我也试过打印,打印图片的时候返回null。检索所有其他字段都可以。我在网络图像中给出了存储在 firebase 数据库中的图像的静态值,它工作正常。但是在动态获取数据时它不起作用。我不知道为什么。我在移动设备上以相同的方式获取数据并且它有效但对于 flutter web 它不起作用。
这是产品型号代码
Stream<List<Product>> get getAllProducts {
return productCollection.get().asStream().map(_productDataFromSnapshot);
}
List<Product> _productDataFromSnapshot(QuerySnapshot snapshot) {
List<Product> productsList = List<Product>();
for (int i = 0; i < snapshot.docs.length; i++) {
productsList.add(Product(
productID: snapshot.docs[i].data()['productID'] ?? "",
name: snapshot.docs[i].data()['name'] ?? "",
volume: snapshot.docs[i].data()['volume'] ?? "",
price: snapshot.docs[i].data()['price'] ?? "",
quantity: snapshot.docs[i].data()['quantity'] ?? "",
brand: snapshot.docs[i].data()['brand'] ?? "",
productAttribute: snapshot.docs[i].data()['productAttribute'] ?? "",
productCtgName: snapshot.docs[i].data()['productCtgName'] ?? "",
productSubCtgName: snapshot.docs[i].data()['productSubCtgName'] ?? "",
productCtgID: snapshot.docs[i].data()['productCtgID'] ?? "",
productSubCtgID: snapshot.docs[i].data()['productSubCtgID'] ?? "",
));
}
return productsList;
}
class Product {
String productID;
String name;
int price;
int salePrice;
DateTime addDate;
String volume;
int quantity;
String productAttribute;
String productSubCtgID;
String productCtgID;
String productSubCtgName;
String productCtgName;
String brand;
String pic;
Product({
this.productID,
this.name,
this.price,
this.addDate,
this.volume,
this.quantity,
this.productAttribute,
this.brand,
this.productCtgID,
this.productSubCtgID,
this.productCtgName,
this.productSubCtgName,
this.pic,
this.salePrice,
});
在 for 循环的 _productDataFromSnapshot 函数中,您没有初始化 pic 字段。
为防止此类错误,请对此类字段使用@required