简体   繁体   English

ListView不在第一次出现Flutter

[英]ListView don't appear in the first time Flutter

My problem is that : The listview does not appear at the first launch of app but after a hot reload it appears. 我的问题是:listview不会在应用程序的首次启动时出现,而是在热重新加载后出现。

It's the first time I meet this bug. 这是我第一次遇到此错误。

That's my code : 那是我的代码:

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

I have an idea but it's strange : before, I using the Visibility widget so it is possible that it comes from a possible "cache" of the application? 我有一个主意,但很奇怪:以前,我使用Visibility小部件,因此它可能来自应用程序的可能“缓存”吗?

All that will look at this post and help me, thank you very much! 所有这些内容都会对这篇文章有所帮助,对我有帮助,非常感谢!

Have a nice day ! 祝你今天愉快 !

Likely because toutesLesCartes.length is 0. 可能是因为toutesLesCartes.length为0。

you can check this using the debugger or display something when the length is 0 eg 您可以使用调试器对此进行检查,或者在长度为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--;
              });
            },
          );
        }
    ),
  );
}

The solution would be to use a FutureBuilder 解决方案是使用FutureBuilder

Word of warning: The application will run the build method multiple times, it's good practice to place anything you don't want to be repeatedly called (like database calls) outside of the method. 警告:应用程序将多次运行build方法,这是将不希望被重复调用的任何内容(例如数据库调用)放置在该方法之外的一种好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM