简体   繁体   English

失败的断言:第 551 行 pos 12:“child.hasSize”:不正确

[英]Failed assertion: line 551 pos 12: 'child.hasSize': is not true

I'm new in Flutter, I'm working on an e-commerce website, I want to add a Grid in my welcome page showing recent products, I'm trying to use GridVeiw.builder, but I got an error我是 Flutter 新手,我正在开发一个电子商务网站,我想在我的欢迎页面中添加一个 Grid 来显示最近的产品,我正在尝试使用 GridVeiw.builder,但出现错误

Failed assertion: line 551 pos 12: 'child.hasSize': is not true.失败的断言:第 551 行 pos 12:“child.hasSize”:不正确。

I don't understand why I'm seeing this error.我不明白为什么我会看到这个错误。

My Welcome Screen我的欢迎屏幕

import 'package:ecommerce_practice/components/carousel.dart';
import 'package:ecommerce_practice/components/horizontal_list.dart';
import 'package:ecommerce_practice/components/products.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class WelcomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('E-commerce'),
        backgroundColor: Colors.red,
        actions: [
          IconButton(
            icon: Icon(
              Icons.search,
            ),
            color: Colors.white,
            onPressed: () {},
          ),
          IconButton(
            icon: Icon(Icons.shopping_cart),
            color: Colors.white,
            onPressed: () {},
          )
        ],
      ),
      drawer: Drawer(
        child: ListView(
          children: [
            UserAccountsDrawerHeader(
              accountName: Text("Aleem"),
              accountEmail: Text('aleem.alam@outlook.com'),
              currentAccountPicture: GestureDetector(
                child: CircleAvatar(
                  backgroundColor: Colors.grey,
                  child: Icon(
                    Icons.person,
                    color: Colors.white,
                  ),
                ),
              ),
              decoration: BoxDecoration(color: Colors.red),
            ),
            DrawerMenuButton(
              title: 'Home',
              icon: Icon(
                Icons.home,
                color: Colors.red,
              ),
            ),
            DrawerMenuButton(
              title: 'My Account',
              icon: Icon(
                Icons.person,
                color: Colors.red,
              ),
            ),
            DrawerMenuButton(
              title: 'My Order',
              icon: Icon(
                Icons.shopping_basket,
                color: Colors.red,
              ),
            ),
            DrawerMenuButton(
              title: 'Category',
              icon: Icon(
                Icons.category,
                color: Colors.red,
              ),
            ),
            DrawerMenuButton(
              title: 'Favourites',
              icon: Icon(
                Icons.favorite,
                color: Colors.red,
              ),
            ),
            Divider(),
            DrawerMenuButton(
              title: 'Settings',
              icon: Icon(
                Icons.settings,
                color: Colors.blue,
              ),
            ),
            DrawerMenuButton(
              title: 'About Us',
              icon: Icon(
                Icons.help,
                color: Colors.blue,
              ),
            ),
          ],
        ),
      ),
      body: ListView(
        children: [
          ImageCarousel(),
          Padding(
            padding: EdgeInsets.all(10.0),
            child: Text('Category'),
          ),
          HorizontalList(),
          Padding(
            padding: EdgeInsets.all(10.0),
            child: Text('Recent Product'),
          ),
          Products(),
        ],
      ),
    );
  }
}

class DrawerMenuButton extends StatelessWidget {
  DrawerMenuButton({this.title, this.icon});
  final String title;
  final Icon icon;
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {},
      child: ListTile(
        title: Text(title),
        leading: icon,
      ),
    );
  }
}


products.dart产品.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Products extends StatefulWidget {
  @override
  _ProductsState createState() => _ProductsState();
}

class _ProductsState extends State<Products> {
  List<dynamic> products = [
    {
      'name': 'Shirt',
      'image': 'images/category.jpg',
      'old_price': 200,
      'price': 140,
    },
    {
      'name': 'Shirt',
      'image': 'images/category.jpg',
      'old_price': 200,
      'price': 140,
    }
  ];
  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      itemCount: products.length,
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
      ),
      itemBuilder: (context, index) {
        return Product(
          name: products[index]['name'],
          image: products[index]['image'],
          oldPrice: products[index]['old_price'],
          finalPrice: products[index]['price'],
        );
      },
    );
  }
}

class Product extends StatelessWidget {
  Product({this.name, this.image, this.oldPrice, this.finalPrice});
  final String name, image;
  final int oldPrice, finalPrice;
  @override
  Widget build(BuildContext context) {
    return Card(
      child: Hero(
        tag: name,
        child: Material(
          child: InkWell(
            onTap: () {},
            child: GridTile(
              footer: Container(
                color: Colors.white,
                child: ListTile(
                  leading: Text(
                    name,
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              child: Image.asset(
                image,
                fit: BoxFit.cover,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

Try setting the shrinkWrap property of the GridView to true so it takes only the space needed based on it's items:尝试将GridViewshrinkWrap属性设置为true以便它仅占用基于其项目所需的空间:

I added a demo using your code as an example:我使用您的代码添加了一个演示作为示例:

 return GridView.builder(
    itemCount: products.length,
    shrinkWrap: true, // new line
    physics: NeverScrollableScrollPhysics(), // new line
    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 2,
    ),
    itemBuilder: (context, index) {
      return Product(
        name: products[index]['name'],
        image: products[index]['image'],
        oldPrice: products[index]['old_price'],
        finalPrice: products[index]['price'],
      );
    },
  );

EDIT: To prevent the GridView from scrolling separately in your ListView , set the physics of the GridView to NeverScrollablePhysics .编辑:要防止GridViewListView单独滚动,请将GridViewphysics设置为NeverScrollablePhysics

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 失败的断言:第 549 行 pos 12:“child.hasSize”:不正确 - Failed assertion: line 549 pos 12: 'child.hasSize': is not true 断言失败:第 1979 行第 12 行:&#39;hasSize&#39; - Failed assertion: line 1979 pos 12: 'hasSize' 颤振错误:断言失败:第 1785 行 pos 12:&#39;hasSize&#39; - Flutter error:Failed assertion: line 1785 pos 12: 'hasSize' Flutter:断言失败:第 1927 行 pos 12:'hasSize' - Flutter: Failed assertion: line 1927 pos 12: 'hasSize' 断言失败:第 6292 行第 12 行:'child == _child':不是真的 - Failed assertion: line 6292 pos 12: 'child == _child': is not true 断言行 5120 pos 12 失败:'child = _child' 不是真的 - Failed assertion line 5120 pos 12: 'child = _child' is not true 失败的断言:第 6075 行 pos 12:&#39;child == _child&#39;:不是真的 - Failed assertion: line 6075 pos 12: 'child == _child': is not true 在“Row”中渲染 ListTile 时出现错误“断言失败:第 1687 行 pos 12:'hasSize'” - Get an error `Failed assertion: line 1687 pos 12: 'hasSize'` when render ListTile inside `Row` 错误:RenderBox 未布局,断言失败:第 1940 行 pos 12:'hasSize' - Error: RenderBox was not laid out, Failed assertion: line 1940 pos 12: 'hasSize' 失败的断言:第 1696 行 pos 12:&#39;center!.parent == this&#39;:不是真的 - Failed assertion: line 1696 pos 12: 'center!.parent == this': is not true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM