Flutter 吸气剂';长度';在null上调用了。接收方:null尝试调用:长度。导致错误的相关小部件是:/bottom\u bar.dart:17:64
对null调用了getter“length”。 收件人:空 尝试呼叫:长度 导致错误的相关小部件是: 纱窗file:///C:/Users/AZMAK/Desktop/material-组件android codelabs/java/flatterprojects/flatter\u login\u screen/lib/pages/bottom\u bar.dart:17:64 我正试图将我的产品发送到卡丁车屏幕上,我有上述错误。 事实上,我将产品添加到一个产品列表中,我想在我的Panier_屏幕小部件中显示这个列表,我可以通过底部栏访问它。但在将产品添加到我的列表后,添加的产品不会出现在我的Panier_屏幕小部件中。怎么办 我的购物车屏幕Flutter 吸气剂';长度';在null上调用了。接收方:null尝试调用:长度。导致错误的相关小部件是:/bottom\u bar.dart:17:64,flutter,dart,Flutter,Dart,对null调用了getter“length”。 收件人:空 尝试呼叫:长度 导致错误的相关小部件是: 纱窗file:///C:/Users/AZMAK/Desktop/material-组件android codelabs/java/flatterprojects/flatter\u login\u screen/lib/pages/bottom\u bar.dart:17:64 我正试图将我的产品发送到卡丁车屏幕上,我有上述错误。 事实上,我将产品添加到一个产品列表中,我想在我的Panier_
import 'package:flutter/material.dart';
import 'package:flutter_login_screen/Entities/Panier.dart';
import 'package:flutter_login_screen/Entities/Produit.dart';
import 'package:flutter_login_screen/pages/panier_item.dart';
class PanierScreen extends StatefulWidget {
final List<Produit> produit;
PanierScreen({Key key,
this.produit }) : super(key: key) ;
@override
_PanierScreenState createState() => _PanierScreenState();
}
class _PanierScreenState extends State<PanierScreen> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return MaterialApp(
title: "Panier",
home: Scaffold(
appBar: AppBar(
title: Text("Panier"),
centerTitle: true,
),
body: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(child: ListView.separated(itemBuilder: (BuildContext context, int index) {
print(widget.produit[index].designation);
return Card (
child: ListTile(
leading: CircleAvatar(
child: FittedBox(
child: Text("${widget.produit[index].prixvente} FCFA"),
),
),
title: InkWell(onTap:() {},
child: Container(child: Text("${widget.produit}".toUpperCase()))),
subtitle: Text("Total : ${widget.produit[index].quantite_vendue * widget.produit[index].prixvente}"),
trailing: Text("${widget.produit[index].quantite_vendue.toString()} x"),
),
);
},
separatorBuilder: (BuildContext context, int index) => Divider(),
itemCount: widget.produit.length))
],
),
),
);
}
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}
导入“包装:颤振/材料.省道”;
导入“包:flatter\u login\u screen/Entities/Panier.dart”;
导入“包:flatter\u login\u screen/Entities/Produit.dart”;
导入“package:flatter_login_screen/pages/panier_item.dart”;
类PanierScreen扩展StatefulWidget{
最终产品清单;
PanierScreen({Key,
this.produit}):super(key:key);
@凌驾
_PanierScreenState createState()=>\u PanierScreenState();
}
类_PanierScreenState使用AutomaticEpaLiveClientMixin扩展状态{
@凌驾
小部件构建(构建上下文){
super.build(上下文);
返回材料PP(
标题:“帕尼尔”,
家:脚手架(
appBar:appBar(
标题:文本(“窗格”),
标题:对,
),
正文:专栏(
mainAxisSize:mainAxisSize.max,
儿童:[
已展开(子项:ListView.separated(itemBuilder:(BuildContext上下文,int索引){
打印(widget.produit[index].designation);
回程卡(
孩子:ListTile(
领先:CircleAvatar(
孩子:FittedBox(
子项:文本(${widget.produit[index].prixvente}FCFA”),
),
),
标题:InkWell(onTap:(){},
子:容器(子:文本(${widget.produit}.toUpperCase()),
字幕:文本(“总计:${widget.produit[index].quantite_vendue*widget.produit[index].prixvente}”),
尾随:文本(${widget.produit[index].quantite_vendue.toString()}x”),
),
);
},
separatorBuilder:(BuildContext上下文,int索引)=>Divider(),
itemCount:widget.produit.length)
],
),
),
);
}
@凌驾
//TODO:实现wantKeepAlive
bool get wantKeepAlive=>true;
}
我的底部栏类小部件:
class AppBottomBar extends StatefulWidget {
AppBottomBar({Key key}): super(key:key);
@override
_AppBottomBarState createState() => _AppBottomBarState();
}
class _AppBottomBarState extends State<AppBottomBar> {
static List<Produit> produit = new List<Produit>() ;
int position = 0;
PageController _pageController = PageController();
List<Widget> _screens = [ HomeScreen(), ConsultationStock(), PanierScreen() ];
int _selectedIndex = 0 ;
void _onPageChanged(int index){
setState(() {
_selectedIndex = index ;
});
}
void _onItemTapped(int selectedIndex) {
_pageController.jumpToPage(selectedIndex);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
controller: _pageController,
children: _screens,
onPageChanged: _onPageChanged,
physics: NeverScrollableScrollPhysics(),
),
bottomNavigationBar: BottomNavigationBar(
// type: BottomNavigationBarType.fixed,
// showUnselectedLabels: false,
// showSelectedLabels: true,
// currentIndex: position,
onTap: _onItemTapped,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home, color: _selectedIndex == 0 ? Colors.blue: Colors.grey,
),
title: Text('Accueil', style: TextStyle(color: _selectedIndex == 0 ? Colors.blue: Colors.grey,))),
BottomNavigationBarItem(icon:
Icon(Icons.shop, color: _selectedIndex == 1 ? Colors.blue: Colors.grey,),
title: Text('Produits', style: TextStyle(color: _selectedIndex == 1 ? Colors.blue: Colors.grey,))),
BottomNavigationBarItem(
icon: Stack(children:<Widget>[ Icon(Icons.shopping_cart, color: _selectedIndex == 2 ? Colors.blue: Colors.grey,), Positioned(
left: 16.0,
child: Icon(Icons.brightness_1, color: Colors.red, size: 9.0,))
]),
title: Text('Panier',style: TextStyle(color: _selectedIndex == 2 ? Colors.blue: Colors.grey,) )),
// BottomNavigationBarItem(icon: Icon(Icons.monetization_on), title: Text('Journal vente')),
// BottomNavigationBarItem(icon: Icon(Icons.settings), title: Text('Paramètres')),
],
)
);
}
}
类AppBottomBar扩展StatefulWidget{
AppBottomBar({Key}):超级(Key:Key);
@凌驾
_AppBottomBarState createState()=>\u AppBottomBarState();
}
类AppBottomBarState扩展了状态{
静态列表produit=新列表();
int位置=0;
PageController_PageController=PageController();
列表屏幕=[HomeScreen()、ConsultationStock()、PanierScreen()];
int _selectedIndex=0;
void _onPageChanged(整型索引){
设置状态(){
_selectedIndex=索引;
});
}
void _onitMapped(int-selectedIndex){
_pageController.jumpToPage(选择索引);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:页面视图(
控制器:_pageController,
孩子们:_屏幕,
onPageChanged:_onPageChanged,
物理学:NeverscrollableScroll物理学(),
),
底部导航栏:底部导航栏(
//类型:BottomNavigationBarType.fixed,
//显示未选择的标签:false,
//showSelectedLabels:true,
//当前索引:位置,
onTap:\u未映射,
项目:[
底部导航气压计(
图标:图标(Icons.home,颜色:_selectedIndex==0?颜色。蓝色:颜色。灰色,
),
标题:文本('Accueil',样式:TextStyle(颜色:_selectedIndex==0?颜色。蓝色:颜色。灰色,),
BottomNavigationBarItem(图标:
图标(Icons.shop,颜色:_selectedIndex==1?Colors.blue:Colors.grey,),
标题:文本(“产品”,样式:TextStyle(颜色:_selectedIndex==1?颜色。蓝色:颜色。灰色,),
底部导航气压计(
图标:堆栈(子项:[图标(Icons.shopping\u cart,颜色:\u selectedIndex==2?颜色。蓝色:颜色。灰色,),已定位(
左:16.0,
子项:图标(Icons.brightness_1,颜色:Colors.red,大小:9.0,)
]),
标题:文本('Panier',样式:TextStyle(颜色:_selectedIndex==2?颜色。蓝色:颜色。灰色,),
//BottomNavigationBarItem(图标:图标(Icons.monetization),标题:文本('Journal vente')),
//BottomNavigationBarItem(图标:图标(Icons.settings),标题:文本('Paramètres')),
],
)
);
}
}
PanierScreen的构造函数有一个名为produit的可选参数
final List<Produit> produit;
PanierScreen({Key key,
this.produit }) : super(key: key)
我看到您的列表produit
在\u AppBottomBarState
中已经是静态的,所以只需按原样传递即可
List<Widget> _screens = [ HomeScreen(), ConsultationStock(), PanierScreen(produit: produit)];
List\u screens=[HomeScreen(),ConsultationStock(),PanierScreen(produit:produit)];
PanierScreen的构造函数有一个名为produit的可选参数
final List<Produit> produit;
PanierScreen({Key key,
this.produit }) : super(key: key)
我看到您的列表produit
在\u AppBottomBarState
中已经是静态的,所以只需按原样传递即可
List<Widget> _screens = [ HomeScreen(), ConsultationStock(), PanierScreen(produit: produit)];
List\u screens=[HomeScreen(),ConsultationStock(),PanierScreen(produit:produit)];
在\u PanierScreenState
构建小部件中,打印(widget.produit.length)
的输出是什么?让我检查一下,获取长度的列表为空,因此它无法获取空值上的长度,这就是它给出错误长度的原因。打印(widget.produit.length)的输出是什么中的
\u窗格屏幕状态生成