颤动多个if陈述

时间:2020-06-01 09:52:15

标签: flutter if-statement dart google-play

在我的flutter应用程序中,我具有以下多个if语句:

        Text(text1).toString()==Text('Info and Comms').toString()?(snapshot.data.documents[8]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Media').toString()?(snapshot.data.documents[10]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Utilities').toString()?(snapshot.data.documents[16]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Education').toString()?(snapshot.data.documents[4]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Repairs').toString()?(snapshot.data.documents[13]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Domestic Help').toString()?(snapshot.data.documents[3]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Transport').toString()?(snapshot.data.documents[15]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Manufacturing').toString()?(snapshot.data.documents[9]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Mining').toString()?(snapshot.data.documents[11]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Supply Chain').toString()?(snapshot.data.documents[14]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Government').toString()?(snapshot.data.documents[6]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Financial and Business Services').toString()?(snapshot.data.documents[5]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Construction').toString()?(snapshot.data.documents[2]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Agriculture').toString()?(snapshot.data.documents[1]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Wholesale and Retail Trade').toString()?(snapshot.data.documents[17]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Accomodation').toString()?(snapshot.data.documents[0]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Health Social and Personal').toString()?(snapshot.data.documents[7]['desc'].toString()).replaceAll("/n", "\n"):
        Text(text1).toString()==Text('Movement').toString()?(snapshot.data.documents[12]['desc'].toString()).replaceAll("/n", "\n"):
        Text('Data not found').toString(),

在移动设备上部署时,它可以完美运行,但在Play商店中启动后,无论如何它都会返回第一条语句。

有什么想法为什么现在可以解决,以及从哪里开始修复?

*****编辑*****

在if语句所在的位置添加了类别页面内容。 原始类别页面代码:

 class CategoryPage extends StatelessWidget {

  final String text1;
  final String text2;
  final String text3;
  CategoryPage(
      {Key key,
      @required this.text1,
      @required this.text2,
      @required this.text3})
      : super(key: key);


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(text2),
        centerTitle: true,
        backgroundColor: Colors.grey,
      ),
      body: ListView(shrinkWrap: true, children: <Widget>[
        Container(
            child: ListTile(
              title: Text(
                'Current Level ' + text3.toString(),
                style: TextStyle(
                    //color: Colors.red[900],
                    color: Text(text3).toString() == Text("5").toString()
                        ? Colors.red[900]
                        : Text(text3).toString() == Text("4").toString()
                            ? Colors.orange[900]
                            : Text(text3).toString() == Text("3").toString()
                                ? Colors.brown[300]
                                : Text(text3).toString() == Text("2").toString()
                                    ? Colors.blue[300]
                                    : Text(text3).toString() == Text("1").toString()
                                        ? Colors.green[300]
                                        : Colors.black,
                    fontSize: 25,
                    fontWeight: FontWeight.bold),
                textAlign: TextAlign.center,
              ),
            ),
          ),
           Container(
            child: ListTile(
              title: Text(
                text1.toString(),
                style: TextStyle(
                    //color: Colors.red[900],
                    color: Colors.white,
                    fontSize: 20,
                    fontWeight: FontWeight.bold),
                textAlign: TextAlign.center,
              ),
            ),
          ),
        Container(
        alignment: Alignment.topLeft,
        padding: const EdgeInsets.all(8.0),
        child: StreamBuilder(
          stream: Text(text3).toString() == Text('1').toString()
              ? Firestore.instance.collection('Level1').snapshots()
              : Text(text3).toString() == Text('2').toString()
                  ? Firestore.instance.collection('Level2').snapshots()
                  : Text(text3).toString() == Text('3').toString()
                      ? Firestore.instance.collection('Level3').snapshots()
                      : Text(text3).toString() == Text('4').toString()
                          ? Firestore.instance.collection('Level4').snapshots()
                          : Text(text3).toString() == Text('5').toString()
                              ? Firestore.instance
                                  .collection('Level5')
                                  .snapshots()
                              : Firestore.instance
                                  .collection('Level1')
                                  .snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Text('Loading data ...');
            } else {
              return Text(
                //snapshot.data.documents[0]['desc'].toString(),
                Text(text1).toString()==Text('Info and Comms').toString()?(snapshot.data.documents[8]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Media').toString()?(snapshot.data.documents[10]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Utilities').toString()?(snapshot.data.documents[16]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Education').toString()?(snapshot.data.documents[4]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Repairs').toString()?(snapshot.data.documents[13]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Domestic Help').toString()?(snapshot.data.documents[3]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Transport').toString()?(snapshot.data.documents[15]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Manufacturing').toString()?(snapshot.data.documents[9]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Mining').toString()?(snapshot.data.documents[11]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Supply Chain').toString()?(snapshot.data.documents[14]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Government').toString()?(snapshot.data.documents[6]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Financial and Business Services').toString()?(snapshot.data.documents[5]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Construction').toString()?(snapshot.data.documents[2]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Agriculture').toString()?(snapshot.data.documents[1]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Wholesale and Retail Trade').toString()?(snapshot.data.documents[17]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Accomodation').toString()?(snapshot.data.documents[0]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Health Social and Personal').toString()?(snapshot.data.documents[7]['desc'].toString()).replaceAll("/n", "\n"):
                Text(text1).toString()==Text('Movement').toString()?(snapshot.data.documents[12]['desc'].toString()).replaceAll("/n", "\n"):
                Text('Data not found').toString(),
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 15,
                    fontWeight: FontWeight.normal),
                textAlign: TextAlign.left,
              );
            }
          },
        ),
        decoration: BoxDecoration(border: Border.all(color: Colors.grey[300])),
      ),]
      ));
  }

}

*****更新***** 在按照Yann39的建议简化了if语句后-从Visual Code部署到我的手机上的代码在我这一边工作得很好,但是从Google Play商店下载该应用程序仍然无法正常工作。

更新的CategoryPage:

class CategoryPage extends StatelessWidget {

  final String text1;
  final String text2;
  final String text3;
  CategoryPage(
      {Key key,
      @required this.text1,
      @required this.text2,
      @required this.text3})
      : super(key: key);


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(text2),
        centerTitle: true,
        backgroundColor: Colors.grey,
      ),
      body: ListView(shrinkWrap: true, children: <Widget>[
        Container(
            child: ListTile(
              title: Text(
                'Current Level ' + text3.toString(),
                style: TextStyle(
                    //color: Colors.red[900],
                    color: Text(text3).toString() == Text("5").toString()
                        ? Colors.red[900]
                        : Text(text3).toString() == Text("4").toString()
                            ? Colors.orange[900]
                            : Text(text3).toString() == Text("3").toString()
                                ? Colors.brown[300]
                                : Text(text3).toString() == Text("2").toString()
                                    ? Colors.blue[300]
                                    : Text(text3).toString() == Text("1").toString()
                                        ? Colors.green[300]
                                        : Colors.black,
                    fontSize: 25,
                    fontWeight: FontWeight.bold),
                textAlign: TextAlign.center,
              ),
            ),
          ),
           Container(
            child: ListTile(
              title: Text(
                text1.toString(),
                style: TextStyle(
                    //color: Colors.red[900],
                    color: Colors.white,
                    fontSize: 20,
                    fontWeight: FontWeight.bold),
                textAlign: TextAlign.center,
              ),
            ),
          ),
        Container(
        alignment: Alignment.topLeft,
        padding: const EdgeInsets.all(8.0),
        child: StreamBuilder(
          stream: Text(text3).toString() == Text('1').toString()
              ? Firestore.instance.collection('Level1').snapshots()
              : Text(text3).toString() == Text('2').toString()
                  ? Firestore.instance.collection('Level2').snapshots()
                  : Text(text3).toString() == Text('3').toString()
                      ? Firestore.instance.collection('Level3').snapshots()
                      : Text(text3).toString() == Text('4').toString()
                          ? Firestore.instance.collection('Level4').snapshots()
                          : Text(text3).toString() == Text('5').toString()
                              ? Firestore.instance
                                  .collection('Level5')
                                  .snapshots()
                              : Firestore.instance
                                  .collection('Level1')
                                  .snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Text('Loading data ...');
            } else {
              return Text(DocumentUtils.getDocumentDescriptionFromTitle(snapshot, text1.toString()),style: TextStyle(
                     color: Colors.white,
                     fontSize: 15,
                     fontWeight: FontWeight.normal),
                 textAlign: TextAlign.left,);
            }
          },
        ),
        decoration: BoxDecoration(border: Border.all(color: Colors.grey[300])),
      ),]
      ));
  }

}

class DocumentUtils {

  static String getDocumentDescriptionFromTitle(AsyncSnapshot snapshot, String title) {
    var map = {
      'Info and Comms':8,
      'Media':10,
      'Utilities':16,
      'Education':4,
      'Repairs':13,
      'Domestic Help':3,
      'Transport':15,
      'Manufacturing':9,
      'Mining':11,
      'Supply Chain':14,
      'Government':6,
      'Financial and Business Services':5,
      'Construction':2,
      'Agriculture':1,
      'Wholesale and Retail Trade':17,
      'Accomodation':0,
      'Health Social and Personal':7,
      'Movement':12,
    };
    return map[title] != null ? snapshot.data.documents[map[title]]['desc'].toString().replaceAll("/n", "\n") : 'Data not found';
  }

}

*****更新******

在简化上述if语句后,将显示正确的类别说明,但现在它仅从Firestore返回Collection'Level1':

stream: Text(text3).toString() == Text('1').toString()
              ? Firestore.instance.collection('Level1').snapshots()
              : Text(text3).toString() == Text('2').toString()
                  ? Firestore.instance.collection('Level2').snapshots()
                  : Text(text3).toString() == Text('3').toString()
                      ? Firestore.instance.collection('Level3').snapshots()
                      : Text(text3).toString() == Text('4').toString()
                          ? Firestore.instance.collection('Level4').snapshots()
                          : Text(text3).toString() == Text('5').toString()
                              ? Firestore.instance
                                  .collection('Level5')
                                  .snapshots()
                              : Firestore.instance
                                  .collection('Level1')
                                  .snapshots(),

当在我的设备上部署应用程序时,上述多个if语句可以很好地工作,只有从Play商店安装应用程序时,我才能通过多个if语句解决这些问题。

1 个答案:

答案 0 :(得分:1)

也许它不能直接回答您的问题(评论太长了),但我不确定您为什么直接使用Text(text1).toString() == Text('text').toString()而不是text1 == 'text'吗?

还要注意,Text(text3).toString()将返回字符串“ Text(text3)”,而不是包含的文本。如果要获取包含的文本,请使用Text(text3).data。但是,您不必再次在这里创建Text小部件,text1 == 'text'应该可以完成工作,它更加简单和高效。

例如,我还将那些if的重复简化为一个函数(假设您在snapshot.data.documents中没有文档标题,那么您将不需要映射来进行映射) :

class DocumentUtils {

  static String getDocumentDescriptionFromTitle(AsyncSnapshot<YourObject> snapshot, String title) {
    var map = {
      'Info and Comms':8,
      'Media':10,
      'Utilities':16,
      'Education':4,
      'Repairs':13,
      'Domestic Help':3,
      'Transport':15,
      'Manufacturing':9,
      'Mining':11,
      'Supply Chain':14,
      'Government':6,
      'Financial and Business Services':5,
      'Construction':2,
      'Agriculture':1,
      'Wholesale and Retail Trade':17,
      'Accomodation':0,
      'Health Social and Personal':7,
      'Movement':12,
    };
    return map[title] != null ? snapshot.data.documents[map[title]]['desc'].toString().replaceAll("/n", "\n") : 'Data not found';
  }

}

因此您只需在小部件中致电DocumentUtils.getDocumentDescriptionFromTitle(snapshot, text1)

如果您的snapshot.data.documents中有文档标题,则类似于:

class DocumentUtils {

  static String getDocumentDescriptionFromTitle(AsyncSnapshot<YourObject> snapshot, String title) {
    return snapshot.data.documents.firstWhere((Document d) => d.title == title)['desc'].replaceAll("/n", "\n");
  }

}