Flutter 错误:RenderBox未布局,断言失败:第1940行位置12:';hasSize';
我无法修复这个错误 RenderBox未布局:RenderPointerListener#2b92a relayoutBoundary=up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flatter/src/rendering/box.dart': 断言失败:第1940行位置12:“hasSize”。 导致错误的相关小部件是->ListView 这是我所有产品的一部分Flutter 错误:RenderBox未布局,断言失败:第1940行位置12:';hasSize';,flutter,flutter-layout,flutter-dependencies,flutter-animation,flutter-test,Flutter,Flutter Layout,Flutter Dependencies,Flutter Animation,Flutter Test,我无法修复这个错误 RenderBox未布局:RenderPointerListener#2b92a relayoutBoundary=up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flatter/src/rendering/box.dart': 断言失败:第1940行位置12:“hasSize”。 导致错误的相关小部件是->ListView 这是我所有产品的一部分 import 'package:flutter/material
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()
],
));
}
}
导入“包装:颤振/材料.省道”;
导入“包装:医院/抽屉/抽屉内容.dart”;
导入“包装:医院/Product/AllProducts/ProductList/Product_list.dart”;
进口“包装:医院/产品/所有产品/旋转木马.省道”;
进口“包装:医院/产品/所有产品/类别.dart”;
类AllProducts扩展StatefulWidget{
@凌驾
_AllProductsState createState()=>\u AllProductsState();
}
类_AllProductsState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:Colors.green,
标题:正文(
“产品”,
样式:TextStyle(fontStyle:fontStyle.italic),
),
行动:[
图标按钮(
图标:图标(Icons.person),
按下:()=>打印(“打开购物车”),
),
],
),
抽屉(
子项:抽屉内容(),
),
正文:ListView(
儿童:[
转盘(),
大小盒子(
身高:10.0,
),
CategoryPage(),
大小盒子(
身高:20.0,
),
产品列表()
],
));
}
}
这是我的产品部分
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(),
);
},
),
);
}
}
导入'dart:convert';
进口“包装:颤振/材料.省道”;
进口“包装:颤振/基础.dart”;
导入“包装:医院/产品/所有产品/产品列表/网络需求dart”;
导入“package:hospital/Product/AllProducts/ProductList/Product_model.dart”;
导入“包:医院/常数.dart”;
将“package:http/http.dart”导入为http;
//导入“../../drawercontent.dart”;
类ProductList扩展了StatefulWidget{
最终s_id;
const ProductList({key,this.s_id}):super(key:key);
@凌驾
_ProductListState createState()=>\u ProductListState();
}
类_ProductListState扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(
//忽略:缺少所需参数
孩子:未来建设者(
//future:NetReq.fetchTeams(),
未来:NetReq.fetchTeams(widget.s_id),
生成器:(上下文,快照){
if(snapshot.hasError){
返回文本(“Error${snapshot.Error}”);
}else if(snapshot.hasData){
返回GridView.count(
//childAspectRatio:1.0,
填充:仅限边缘设置(左:20,右:20),
交叉轴计数:2,
横轴间距:18,
平均间距:18,
子项:snapshot.data.map((团队){
回程卡(
标高:3.0,
保证金:所有边缘套(10.0),
形状:圆形矩形边框(
边界半径:边界半径。圆形(12.0),
),
子:容器(
宽度:150.0,
填充:边缘设置。全部(8.0),
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
扩大(
孩子:ClipRRect(
边界半径:边界半径。圆形(14.0),
子:容器(
装饰:盒子装饰(
边界半径:边界半径。圆形(12.0),
图像:装饰图像(
图片:NetworkImage(
"https://5.imimg.com/data5/MR/TK/KJ/SELLER-769696/tulac-granules-500x500.jpeg"),
),
),
),
),
),
尺寸箱(高度:12.0),
正文(
//“个人用郁金香颗粒,包装尺寸:90克”,
team.teamUniqId,
溢出:TextOverflow.省略号,
maxLines:2,
风格:kTitleStyle),,
尺寸箱(高度:6.0),
正文(
//“\u20B9 239/盒”,
team.teamType,
maxLines:1,
风格:kSubTitleStyle),
],
),
),
);
}).toList());
}
返回中心(
子对象:CircularProgressIndicator(),
);
},
),
);
}
}
当ListView没有约束高度(使其获得无限高度)时,会发生这种情况,您可以使用两种解决方案来解决此问题
包覆面提取:true
作为参数
这将告诉ListView使用尽可能小的空间
SizedBox
或Container
)包装ListView,并为其指定如下高度和宽度
容器(
身高:50,
宽度:50,
子:ListView()
)
请添加GridView包覆面提取:true,如下所示:
返回GridView.count(
包覆膜收缩:对
填充:仅限边缘设置(左:20,右:20),
交叉轴计数:2,
横轴间距:18,
平均间距:18,
); 谢谢,这是工作