【问题标题】:ListView don't appear in the first time FlutterListView 在第一次 Flutter 中不出现
【发布时间】:2019-11-16 00:39:31
【问题描述】:

我的问题是:列表视图在第一次启动应用程序时没有出现,但在热重载后出现。

这是我第一次遇到这个错误。

这是我的代码:

@override
Widget build(BuildContext context) {
  return new Scaffold(
    backgroundColor: Colors.black,
    body: new ListView.builder(
        itemCount: toutesLesCartes.length,
        itemBuilder: (context, count) {
          Carte carte = toutesLesCartes[count];
          String nom = carte.nomCarte;
          String image = carte.imageCarte;
          return new Dismissible(
            key: new Key(nom),
            direction: DismissDirection.endToStart,
            child: new Container(
              margin: EdgeInsets.all(5.0),
              child: new Card(
                color: Colors.transparent,
                elevation: 10.0,
                child: new Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    new Image.network(image, width: 150.0, height: 150.0,),
                    new CustomText(nom, factor: 2.0,),
                  ],
                ),
              ),
            ),
            background: new Container(
              padding: EdgeInsets.only(right: 20.0),
              color: Colors.red,
              child: new Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  new CustomText("Supprimer"),
                  new Icon(Icons.delete, color: Colors.white,)
                ],
              ),
            ),
            onDismissed: (direction) {
              setState(() {
                toutesLesCartes.removeAt(count);
                nombreCarteChoisiValeur--;
              });
            },
          );
        }
    ),
  );
}

我有一个想法,但很奇怪:之前,我使用 Visibility 小部件,所以它可能来自应用程序的可能“缓存”?

所有会看这篇文章并帮助我的人,非常感谢!

祝你有美好的一天!

【问题讨论】:

    标签: android-studio listview flutter dart


    【解决方案1】:

    可能是因为 toutesLesCartes.length 为 0。

    您可以使用调试器检查这一点,或者在长度为 0 时显示一些内容,例如

    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        backgroundColor: Colors.black,
        body: new ListView.builder(
            itemCount: toutesLesCartes.length, //* place breakpoint here
            itemBuilder: (context, count) {
              Carte carte = toutesLesCartes[count];
              String nom = carte.nomCarte;
              String image = carte.imageCarte;
    
            if(toutesLesCartes == null || toutesLesCartes.length == 0){
              return CircularProgressIndicator(); //you should see loading animation if list is empty
            }
              return new Dismissible(
                key: new Key(nom),
                direction: DismissDirection.endToStart,
                child: new Container(
                  margin: EdgeInsets.all(5.0),
                  child: new Card(
                    color: Colors.transparent,
                    elevation: 10.0,
                    child: new Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        new Image.network(image, width: 150.0, height: 150.0,),
                        new CustomText(nom, factor: 2.0,),
                      ],
                    ),
                  ),
                ),
                background: new Container(
                  padding: EdgeInsets.only(right: 20.0),
                  color: Colors.red,
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      new CustomText("Supprimer"),
                      new Icon(Icons.delete, color: Colors.white,)
                    ],
                  ),
                ),
                onDismissed: (direction) {
                  setState(() {
                    toutesLesCartes.removeAt(count);
                    nombreCarteChoisiValeur--;
                  });
                },
              );
            }
        ),
      );
    }
    

    解决方案是使用FutureBuilder

    警告:应用程序将多次运行 build 方法,最好将您不想被重复调用的任何内容(如数据库调用)放在该方法之外。

    【讨论】:

    • 您好 F-1,感谢您的帮助。不,我的列表是由 initState() 发起的,所以她不是空的。但是如果我改变我的方向设备,列表视图就会出现。太奇怪了。那是我的设备的屏幕截图: App first launch -> i.stack.imgur.com/Tk6hw.jpg App 第一次启动后和我更改方向设备时 -> i.stack.imgur.com/tIJdR.jpg
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多