gpt4 book ai didi

flutter - flutter 多个if陈述

转载 作者:行者123 更新时间:2023-12-03 04:46:53 25 4
gpt4 key购买 nike

我的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语句才会给我带来这些问题。

最佳答案

也许它不能直接回答您的问题(评论太长了),但是我不确定您为什么直接使用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");
}

}

关于flutter - flutter 多个if陈述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62129618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com