【问题标题】:The getter 'length' was called on null. Receiver: null Tried calling: length. The relevant error-causing widget was: /bottom_bar.dart:17:64在 null 上调用了 getter 'length'。接收者:null 尝试调用:长度。相关的导致错误的小部件是:/bottom_bar.dart:17:64
【发布时间】:2020-10-30 10:54:16
【问题描述】:

在 null 上调用了 getter 'length'。 接收方:空 尝试调用:长度 相关的导致错误的小部件是: PanierScreen file:///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 Widget 中。怎么办?

我的购物车屏幕

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

【问题讨论】:

  • print(widget.produit.length)_PanierScreenState 构建小部件中的输出是什么?
  • 让我检查一下
  • 您获取长度的列表为 null,因此它无法获取 null 上的长度,这就是它给出错误长度的原因是在 null 上调用。

标签: flutter dart


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2022-11-14
    相关资源
    最近更新 更多