flutter - 无效的参数:URL文件:///Trousers中未指定主机

标签 flutter dart flutter-layout flutter-dependencies

我是新来的。
URL中的图像未显示在屏幕上。
错误是指定的URL中没有主机。我目前正在从教程中学习,但是讲师的代码运行良好,而我的却没有。请帮我,因为我是新手。
这是我的Main.dart文件:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MyShop',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ProductsOverviewScreen(),
    );
  }
}
products_overview_screen.dart
 class ProductsOverviewScreen extends StatelessWidget { 
  final List<Product> loadedProducts = [ 
    Product(
      id: 'p1', 
      title: 'Red Shirt', 
      description: 'A red shirt - it is pretty red!', 
      price: 29.99, 
      imageUrl: 
          'https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg', 
    ),
    Product( `
      id: 'p2', 
      title: 'Trousers',
      description: 'A nice pair of trousers.', 
      price: 59.99, 
      imageUrl:
  'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Trousers%2C_dress_%28AM_1960.022-8%29.jpg/512px-Trousers%2C_dress_%28AM_1960.022-8%29.jpg',
    ), 
    Product( 
      id: 'p3', 
      title: 'Yellow Scarf', 
      description: 'Warm and cozy - exactly what you need for the winter.', 
      price: 19.99, 
      imageUrl: 
          'https://live.staticflickr.com/4043/4438260868_cc79b3369d_z.jpg', 
    ), 
    Product(
      id: 'p4',
      title: 'A Pan',
      description: 'Prepare any meal you want.',
      price: 49.99, 
      imageUrl: 
          'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Cast-Iron-Pan.jpg/1024px-Cast-Iron- Pan.jpg',
    ), 
  ];

  @override 
  Widget build(BuildContext context) { 
    return Scaffold( 
      appBar: AppBar( 
        title: Text('My Shop'),
      ),
      body: GridView.builder(
        padding: const EdgeInsets.all(10.0),
        itemCount: loadedProducts.length,
        itemBuilder: (ctx, i) => ProductItem(
          loadedProducts[i].id,
          loadedProducts[i].title,
          loadedProducts[i].imageUrl,
        ),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: 3 / 2,
          crossAxisSpacing: 10,
          mainAxisSpacing: 10,
        ),
      ),
    );
  }
} 
这是Product_item.dart
class ProductItem extends StatelessWidget {
  final String id;
  final String title;
  final String imageUrl;
ProductItem(this.id, this.imageUrl, this.title);
  @override
  Widget build(BuildContext context) {
    return GridTile(
      child: Image.network(
        //imageUrl
        imageUrl,
        fit: BoxFit.cover,
      ),
    );
  }
} ```

这个product.dart
class Product {
  final String id;
  final String title;
  final String description;
  final double price;
  final String imageUrl;
  bool isFavorite;

  Product({
    @required this.id,
    @required this.description,
    @required this.imageUrl,
              this.isFavorite,
    @required this.price,
    @required this.title,
  });
} ```

最佳答案

问题出在你的ProductItem类上。您尚未按照数字定义方式分配项目。

Like: ProcutItem(this.id, this.title, this.imageUrl), the item should also be passed in that same manner. No changes in that


解决方案
class ProductItem extends StatelessWidget {
  final String id;
  final String title;
  final String imageUrl;
  ProductItem(this.id, this.title, this.imageUrl);  // <---- Here is the change
  @override
  Widget build(BuildContext context) {
    return GridTile(
      child: Image.network(
        //imageUrl
        imageUrl,
        fit: BoxFit.cover,
      ),
    );
  }
}
从那以后,您已经通过这种方式在ProductOverViewScreen中传递了该项目
        // first should be id, then title, then imageURl. It should match from the above class
        ProductItem(
          loadedProducts[i].id,  
          loadedProducts[i].title,
          loadedProducts[i].imageUrl,
        )
ASSUMPTIONS
我的Product类看起来像这样,我敢肯定您可能仅以这种方式创建了该类。您的Product类在代码中不可用,因此是假设。如果需要,可以引用此代码
class Product{
  double price;
  String id, title, description, imageUrl;
  
  Product({this.price, this.id, this.title, this.description, this.imageUrl});
}
结果
Result Screenshot

关于flutter - 无效的参数:URL文件:///Trousers中未指定主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63736715/

相关文章:

flutter - 当用户在 flutter 中按下按钮时,如何添加另一个文本框?

firebase - Flutter/Firebase-文档 ID 未知时如何执行删除文档功能?

Dart Async 不等待完成

dart - 如何将图像背景分配给 flutter 中的列

flutter - 根据内容将 TextField 高度扩展到特定行

Flutter Url Launcher 不会将整个文本传递给短信

flutter - 为什么在构建 flutter web 时出现白屏

dart - 如何编写Dart 'worker'服务器

javascript - 在 Dart 中创建 Cesium 图像提供程序

flutter - 弹出式作用域小部件仅适用于我项目的最后一个屏幕,我想在每个页面或特定页面上使用它