flutter - getter 'length'在null上被调用。接收方:空尝试调用:长度。相关的引起错误的小部件是:/bottom_bar.dart:17:64

标签 flutter dart

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')),
        ],
    )
    );
  }
}

最佳答案

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)];

关于flutter - getter 'length'在null上被调用。接收方:空尝试调用:长度。相关的引起错误的小部件是:/bottom_bar.dart:17:64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62821735/

相关文章:

flutter - 占行空间的一小部分

android - 在 ListView 中显示图像时内存不足

flutter - flutter 的小怪未检测到

firebase - Flutter Firebase存储图像检索流

android - 如何检查按钮是否被按下

android - Flutter Android 构建因 middles/compiled_local_resources/debug/out 失败,不是可读目录

android - 在 Flutter 中更新 MaterialPageRoute 的状态

flutter 火力基地 I/BufferQueueProducer queueBuffer : slot 0 is dropped, handle=0x79c7c5ec00

dart - Flutter Future 和 setstate

android - 在 Flutter 中使用 showModalBottomSheet() 方法构建的底部工作表上显示的小部件中未更新变量