flutter - 在 null 上调用了 getter 'visible'
问题描述
我正在尝试创建一个包含搜索栏、无限滚动产品页面和按钮(添加产品、删除产品和通知)的页面。以下语句有错误...
The getter 'visible' was called on null,
Receiver : null,
Tried calling : null
请帮助我纠正此代码。
class Inventory extends StatefulWidget {
@override
_InventoryState createState() => _InventoryState();
}
class _InventoryState extends State<Inventory> {
Color _color = Colors.white;
@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: PreferredSize(
preferredSize: Size.fromHeight(70),
child: AppBar(
backgroundColor: Theme.Colors.darkBlue,
iconTheme: IconThemeData(size: 10, color: Colors.white),
elevation: 0,
title: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
AutoSizeText(
"Inventory ",
style: TextStyle(
fontSize: 35,
),
),
],
)),
),
drawer: MainDrawer(),
backgroundColor: Color(0xFF09182C),
body: Center(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
height: 70,
width: width * 0.76,
child: TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.all(10.0),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
hintText: 'Search Inventory',
hintStyle:
TextStyle(color: Color(0xFFE0E0E0), fontSize: 20),
suffixIcon: Icon(
Icons.search,
color: Colors.white,
size: 30,
)),
style: TextStyle(color: _color),
),
),
Container(
padding: EdgeInsets.only(bottom: 30.0),
child: IconButton(
onPressed: () {},
icon: Icon(Icons.filter_list,
size: 40, color: Color(0xFFFFAE40)),
),
),
],
),
LoadingPage(),
Spacer(),
Container(
margin: EdgeInsets.only(bottom: 70),
padding: EdgeInsets.only(left: 30, right: 30),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Material(
type: MaterialType.transparency,
child: Ink(
decoration: BoxDecoration(
border:
Border.all(color: Color(0xFF57D0F4), width: 2.0),
color: Colors.transparent,
shape: BoxShape.circle,
),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddProduct()),
);
},
child: Padding(
padding: EdgeInsets.all(13.0),
child: Icon(
Icons.add,
size: 55.0,
color: Color(0xFF57D0F4),
),
),
),
),
),
Material(
type: MaterialType.transparency,
child: Ink(
decoration: BoxDecoration(
border:
Border.all(color: Color(0xFF57D0F4), width: 2.0),
color: Colors.transparent,
shape: BoxShape.circle,
),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => RemoveProduct()),
);
},
child: Padding(
padding: EdgeInsets.all(13.0),
child: Icon(
Icons.delete_outline,
size: 55.0,
color: Color(0xFF57D0F4),
),
),
),
),
),
Material(
type: MaterialType.transparency,
child: Ink(
decoration: BoxDecoration(
border:
Border.all(color: Color(0xFF57D0F4), width: 2.0),
color: Colors.transparent,
shape: BoxShape.circle,
),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Notify()),
);
},
child: Padding(
padding: EdgeInsets.all(13.0),
child: Icon(
Icons.notifications_none,
size: 55.0,
color: Color(0xFF57D0F4),
),
),
),
),
),
],
),
)
],
),
));
}
}
class LoadingPage extends StatefulWidget {
@override
_LoadingPageState createState() => _LoadingPageState();
}
class _LoadingPageState extends State<LoadingPage> {
List prd;
ScrollController _scrollController = ScrollController();
int _currentMax = 10;
@override
void initState() {
super.initState();
prd = List.generate(10, (i) => "Product ${i + 1}");
_scrollController.addListener(() {
if (_scrollController.position.pixels ==
_scrollController.position.maxScrollExtent) {
_getMoreData();
}
});
}
_getMoreData() {
for (int i = _currentMax; i < _currentMax + 10; i++) {
return prd.add("Product ${i + 1}");
}
_currentMax = _currentMax + 10;
setState(() {});
}
@override
Widget build(BuildContext context) {
return ListView.builder(
controller: _scrollController,
itemCount: prd.length + 1,
itemExtent: 70,
itemBuilder: (context, i) {
if (i == prd.length) {
return CupertinoActivityIndicator();
}
return ListTile(
title: AutoSizeText(prd[i]),
);
});
}
}
解决方案
感谢@Mobina 和@JohnJoe 的帮助。我用 Flexible 包装了 ListView.builder 解决了问题并添加了容器以将其放入灰色背景中。
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.only(top: 10.0, bottom: 10.0),
height: 450,
width: 350,
decoration: BoxDecoration(
color: Color(0xFF707070),
borderRadius: BorderRadius.all(
Radius.circular(
10.0,
),
)),
child: Flexible(
child: ListView.builder(
controller: _scrollController,
itemCount: prd.length + 1,
itemExtent: 42.5,
itemBuilder: (context, i) {
if (i == prd.length) {
return CupertinoActivityIndicator();
}
return ListTile(
title: AutoSizeText(prd[i]),
);
}),
),
);
}
推荐阅读
- sql - 我在尝试检查时遇到 sql 错误(sales id NOT IN sales details 以检查是否有任何没有详细信息的销售)
- frontend - 多个前端(客户端)相同的后端
- android - React Native 本地资产图像在发布模式下未显示一段时间
- php - 为什么 Session 在服务器上不起作用,是否有任何服务器端设置?
- javascript - 使用 javascript 使用数组过滤具有键和值对的对象
- javascript - 从数组的各个部分计算减少总数?
- amazon-web-services - AWS cognito 用户池的 EmailConfiguration
- php - laravel 6 + elasticsearch-php 7.6 + xampp:在您的集群中找不到活动节点
- java - 为@ApiResponse 设置自定义示例
- c# - 使用 NUnit 模拟 EF 的 ExecuteSqlCommand