无法加载资源(图像)颤振



这是我的API代码,我正在从这里传递图像路径到flutter以确定要加载的图像。然而,所有其他数据,如价格或标题都在加载,但图像没有加载,并抛出无法加载资产的错误。

app.post('/products',(req,res)=>{
//console.log(req.body.LanguageID);
//console.log(req.body.CategoryID);
console.log(req.body);
res.status(200).json({
"StatusCode": 200,
"StatusMessage": "success",
"Data": [
{
"Id": "p8",
"title": "Sufiyan",
"ImageAsset": "assets/images/plant3.png",
"price": 80.0                
},
{
"Id": "p8",
"title": "Sufiyan",
"ImageAsset": "assets/images/plant1.png",
"price": 90.0
}
],
"Message": null,
// "localId": '8',
// "idToken": '',
// "error": {
//     "message": ""
// },
// "expiresIn": "9"
});
});
app.listen(5000);

这就是我如何从提供商加载数据到小部件,无法理解为什么这是不工作,因为我是一个新手扑动和无法理解它。

@override
void initState() {
// TODO: implement initState
super.initState();
//gettingData();
final productData = Provider.of<Products>(context, listen: false);
productData.fetchData(context);
// Create TabController for getting the index of current tab
_controller = TabController(length: list.length, vsync: this);
_containerheight = _totalarrayheight(8) * 140;
}
@override
Widget build(BuildContext context) {
final productData = Provider.of<Products>(context);
final products = productData.items;
final featuredProducts = productData.featuredItems;
_controller!.addListener(() {
setState(() {
_selectedIndex = _controller!.index;
if (_controller!.index == 0) {
_containerheight =
140 * _totalarrayheight(products.length.toDouble());
} else {
_containerheight =
140 * _totalarrayheight(featuredProducts.length.toDouble());
}
});
if (kDebugMode) {
print("Selected Index: " + _controller!.index.toString());
}
});

这是我的提供者代码:

class Products with ChangeNotifier {
late List<ProductList> _items = [];
fetchData(context) async {
_items = await getData();
notifyListeners();
}

为产品

创建网格的小部件
class NewProductGrid extends StatelessWidget {
const NewProductGrid({
Key? key,
required this.products,
}) : super(key: key);
final List<ProductList> products;
@override
Widget build(BuildContext context) {
return StaggeredGridView.countBuilder(
padding: EdgeInsets.only(right: 30, left: 10, top: 10),
physics:
NeverScrollableScrollPhysics(), // to disable GridView's scrolling
shrinkWrap: false,
crossAxisCount: 4,
itemCount: products.length,
itemBuilder: (BuildContext context, int index) => GestureDetector(
onTap: () {
Navigator.of(context).pushNamed(ProductDetail.routeName,
arguments: {'id': products[index].id});
},
child: Container(
//padding: EdgeInsets.only(left:40.0),
child: Column(
children: <Widget>[
Stack(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 35.0),
child: Container(
height: 150,
width: 150,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: primary_background_six,
),
),
),
Container(
child: Image.asset(
"${products[index].imageAsset}",
fit: BoxFit.cover,
)),
Container(),
],
),
Padding(
padding: const EdgeInsets.only(left: 20.0),
child: Text(
"$${products[index].price.toInt()}",
style: TextStyle(fontFamily: 'Morganite Light', fontSize: 50),
),
),
Padding(
padding: const EdgeInsets.only(left: 20.0),
child: Text(
"${products[index].title}",
style: TextStyle(fontFamily: 'PlayFairDisplay', fontSize: 18),
),
),
],
),
),
),
staggeredTileBuilder: (int index) =>
StaggeredTile.count(2, index.isEven ? 3 : 3),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
);
}
}

调用小部件的屏幕代码。

Container(
height: _containerheight,
child: TabBarView(
controller: _controller,
children: <Widget>[
Container(
//height: 140.0 * _totalarrayheight(tagObjs.length.toDouble()),
child: NewProductGrid(
products: products),
)

产品清单型号代码:

import 'package:flutter/material.dart';
class ProductList with ChangeNotifier {
final String id;
final String title;
final String imageAsset;
final num price;
bool isFavorite;
ProductList({
required this.id,
required this.title,
required this.price,
required this.imageAsset,
this.isFavorite = false,
});
factory ProductList.fromMap(Map<String, dynamic> json) {
return ProductList(
id: json['id'],
title: json['title'],
imageAsset: json['imageAsset'],
price: json['price']);
}
factory ProductList.fromJson(Map<String, dynamic> json) {
print('Json:');
print(json);
return ProductList(
id: json['id'] ?? '',
title: json['title'] ?? '',
imageAsset: json['imageAsset'] ?? '',
price: json['price'] ?? 0);
}
void toggleFavoriteStatus() {
isFavorite = !isFavorite;
notifyListeners();
}
}

响应中的图像资产为大写i,产品列表模型的图像资产为小写i

将模型更改为

import 'package:flutter/material.dart';
class ProductList with ChangeNotifier {
final String id;
final String title;
final String imageAsset;
final num price;
bool isFavorite;
ProductList({
required this.id,
required this.title,
required this.price,
required this.imageAsset,
this.isFavorite = false,
});
factory ProductList.fromMap(Map<String, dynamic> json) {
return ProductList(
id: json['id'],
title: json['title'],
imageAsset: json['ImageAsset'],
price: json['price']);
}
factory ProductList.fromJson(Map<String, dynamic> json) {
print('Json:');
print(json);
return ProductList(
id: json['id'] ?? '',
title: json['title'] ?? '',
imageAsset: json['ImageAsset'] ?? '',
price: json['price'] ?? 0);
}
void toggleFavoriteStatus() {
isFavorite = !isFavorite;
notifyListeners();
}
}

相关内容

  • 没有找到相关文章

最新更新