flutter - 错误:未布置 RenderBox,断言失败:第 1940 行 pos 12:'hasSize'
问题描述
我无法修复此错误
RenderBox 未布置:RenderPointerListener#2b92a relayoutBoundary=up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart':断言失败:第 1940 行 pos 12:'hasSize'。相关的导致错误的小部件是 ->ListView
这是我的 allProducts 部分。
import 'package:flutter/material.dart';
import 'package:hospital/Drawer/drawercontent.dart';
import 'package:hospital/Product/AllProducts/ProductList/product_list.dart';
import 'package:hospital/Product/AllProducts/carousel.dart';
import 'package:hospital/Product/AllProducts/category.dart';
class AllProducts extends StatefulWidget {
@override
_AllProductsState createState() => _AllProductsState();
}
class _AllProductsState extends State<AllProducts> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: Text(
"Product",
style: TextStyle(fontStyle: FontStyle.italic),
),
actions: [
IconButton(
icon: Icon(Icons.person),
onPressed: () => print("open cart"),
),
],
),
drawer: Drawer(
child: DrawerContent(),
),
body: ListView(
children: [
Carousel(),
SizedBox(
height: 10.0,
),
CategoryPage(),
SizedBox(
height: 20.0,
),
ProductList()
],
));
}
}
这是我的产品部分
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:hospital/Product/AllProducts/ProductList/network_req.dart';
import 'package:hospital/Product/AllProducts/ProductList/product_model.dart';
import 'package:hospital/constant.dart';
import 'package:http/http.dart' as http;
// import '../../drawercontent.dart';
class ProductList extends StatefulWidget {
final s_id;
const ProductList({key, this.s_id}) : super(key: key);
@override
_ProductListState createState() => _ProductListState();
}
class _ProductListState extends State<ProductList> {
@override
Widget build(BuildContext context) {
return Container(
// ignore: missing_required_param
child: FutureBuilder<List<Model>>(
// future: NetReq.fetchTeams(),
future: NetReq.fetchTeams(widget.s_id),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text("Error ${snapshot.error}");
} else if (snapshot.hasData) {
return GridView.count(
// childAspectRatio: 1.0,
padding: EdgeInsets.only(left: 20, right: 20),
crossAxisCount: 2,
crossAxisSpacing: 18,
mainAxisSpacing: 18,
children: snapshot.data.map((team) {
return Card(
elevation: 3.0,
margin: EdgeInsets.all(10.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
child: Container(
width: 150.0,
padding: EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
child: ClipRRect(
borderRadius: BorderRadius.circular(14.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
image: DecorationImage(
image: NetworkImage(
"https://5.imimg.com/data5/MR/TK/KJ/SELLER-769696/tulac-granules-500x500.jpeg"),
),
),
),
),
),
SizedBox(height: 12.0),
Text(
// "Tulac Granules, For Personal, Packaging Size: 90 Gm",
team.teamUniqId,
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: kTitleStyle),
SizedBox(height: 6.0),
Text(
// "\u20B9 239/ Box",
team.teamType,
maxLines: 1,
style: kSubTitleStyle),
],
),
),
);
}).toList());
}
return Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}
解决方案
当 ListView 没有限制高度时会发生这种情况,这使得它获得无限高度,您可以使用两种解决方案来解决这个问题
添加
shrinkWrap: true
作为参数,它将告诉 ListView 使用尽可能少的空间,用一个受限制的高度小部件(例如 a
SizedBox
或 a )包装您的 ListViewContainer
,并像这样给它一个高度和宽度
Container(
height: 50,
width: 50,
child: ListView()
)
希望这能解决你的问题
推荐阅读
- node.js - 如何使用 babel 构建非 js 文件?
- php - 填写多个值php mysql
- ios - iOS 10 顶部的 UITableView 空间?
- linux - 从 Windows 上的 spark 存储文件到 HDFS
- php - 访问 WC() 以在 ajax 回调中获取购物车内容
- swift - 我在哪里可以找到媒体按钮以将我的资产放到情节提要中
- oop - 支持字段公开时自动生成getter和setter的目的是什么
- python - Django中的简单查询
- java - ncpu.s 文件加载到错误的 'utf-8' android studio
- c# - .netcore Web 应用程序因“提供给函数的缓冲区太小”错误而失败