[英]The getter 'length' was called on null. Receiver: null Tried calling: length. The relevant error-causing widget was: /bottom_bar.dart:17:64
The getter 'length' was called on null.在 null 上调用了 getter 'length'。 Receiver: null Tried calling: length The relevant error-causing widget was: PanierScreen file:///C:/Users/AZMAK/Desktop/material-components-android-codelabs/java/FlutterProjects/flutter_login_screen/lib/pages/bottom_bar.dart:17:64.接收方:null 尝试调用:length 相关的导致错误的小部件是:PanierScreen file:///C:/Users/AZMAK/Desktop/material-components-android-codelabs/java/FlutterProjects/flutter_login_screen/lib/pages/bottom_bar。 dart:17:64。
I'm trying to send my product into a screen kart and I have the above error.我正在尝试将我的产品发送到屏幕卡丁车中,但出现上述错误。 Indeed I add products to a list of products and I would like to display this list in my Panier_Screen widget which I can access thanks to a BottomBar.事实上,我将产品添加到产品列表中,并且我想在我的 Panier_Screen 小部件中显示这个列表,我可以通过 BottomBar 访问它。 But after adding the product to my list, the added product does not appear in my Panier_Screen Widget.但是在将产品添加到我的列表后,添加的产品并没有出现在我的 Panier_Screen Widget 中。 What to do?该怎么办?
My Cart Screen我的购物车屏幕
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;
}
My BottomBar class widget:我的 BottomBar class 小部件:
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')),
],
)
);
}
}
The constructor of PanierScreen has an optional parameter called produit PanierScreen 的构造函数有一个名为 produit 的可选参数
final List<Produit> produit;
PanierScreen({Key key,
this.produit }) : super(key: key)
But when creating it in _AppBottomBarState
you don't give it a value so it initializes with a null value但是在_AppBottomBarState
中创建它时,您没有给它一个值,因此它使用 null 值进行初始化
List<Widget> _screens = [ HomeScreen(), ConsultationStock(), PanierScreen() ]; //produit = null if you don't explicitly give it a value
I see your List produit
is already static in the _AppBottomBarState
so just pass it as it is我看到您的 List produit
已经是 _AppBottomBarState 中的_AppBottomBarState
所以只需按原样传递
List<Widget> _screens = [ HomeScreen(), ConsultationStock(), PanierScreen(produit: produit)];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.