getter'length'被调用为null。 接收者:null 尝试通话:时长 相关的引起错误的小部件是: PanierScreen文件:/// C:/Users/AZMAK/Desktop/material-components-android-codelabs/java/FlutterProjects/flutter_login_screen/lib/pages/bottom_bar.dart:17:64。
我正在尝试将我的产品发送到屏幕卡丁车中,但出现上述错误。 实际上,我已将产品添加到产品列表中,并且希望在Panier_Screen小部件中显示此列表,这要归功于BottomBar。但是将产品添加到我的列表后,添加的产品不会出现在我的Panier_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;
}
我的BottomBar类窗口小部件:
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')),
],
)
);
}
}
答案 0 :(得分:0)
PanierScreen的构造函数具有一个称为produit的可选参数
final List<Produit> produit;
PanierScreen({Key key,
this.produit }) : super(key: key)
但是在_AppBottomBarState
中创建它时,您没有赋予它一个值,因此它以空值初始化
List<Widget> _screens = [ HomeScreen(), ConsultationStock(), PanierScreen() ]; //produit = null if you don't explicitly give it a value
我看到您的列表produit
在_AppBottomBarState
中已经是静态的,因此只需按原样传递
List<Widget> _screens = [ HomeScreen(), ConsultationStock(), PanierScreen(produit: produit)];