- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的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(),
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])),
),]
));
}
}
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';
}
}
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(),
最佳答案
也许它不能直接回答您的问题(评论太长了),但是我不确定您为什么直接使用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/
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
显然,我实际上是想创建一个数组,在多次试验 (5000) 中随机分配生日。然后假设每次有 2 - 50 人至少有 2 个生日时,将结果除以 5,000 以获得近似概率。我相信我的循环搞砸了,希望得到一
PYTHON:试图计算摆在不同行星上摆动所需的时间,我对此非常陌生,因此可能有一个简单的解决方案: def main(): print('Period of a pendulum')
在我的 MySQL 表中,有一行标记为“state”。有 3 个可能的值。我如何利用 PHP 将“Unbanned”回显为状态中的“0”?将“禁止”与“1”相对应,将“临时禁止”与“3”相对应? 我如
我的宏代码中有一个 If 公式,但它似乎没有以正确的方式工作。 下面的代码计算 rCell.Offset 并始终将其应用为 True,我有一些示例,其中它等于 112、113、118 等,并且它正在将
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: Why do you have to put a 1; at the end of a Perl 5 module?
网上查了一下parent::init();的意思.我所能找到的只是 init() 是初始化一些设置,这些设置希望在每次应用程序运行时出现。谁能准确解释 parent::init() 的含义,比如这两个
这个问题已经有答案了: What does "%.*s" mean in printf? (4 个回答) 已关闭 3 年前。 我试图理解 printf() 但我只熟悉 C++ cout= 0; r--
我正在开发一个试图通过 tsv infile 将数据插入 mysql 数据库的 java 项目。运行该程序时出现以下错误: Exception in thread "main" java.sql.SQ
好的,我正在开发一个新的应用程序,只要我使用相对 View ,一切都运行良好。但是,我想要一个选项卡式布局,所以我将我拥有的内容(到目前为止还不多,因为我刚刚从数据库中读取它并且设置正常)切换到选项卡
我在另一个问题中看到了这个提示,想知道是否有人可以向我解释这究竟是如何工作的? try { return x; } finally { x = null; } 我的意思是,finally 子句是否真的
这个问题已经有答案了: Is it better to wrap code into an 'IF' statement, or is it better to 'short circuit' the
我是一名优秀的程序员,十分优秀!