【发布时间】:2021-04-24 22:42:14
【问题描述】:
我想在此代码中添加一个条件表达式,以便在 Firebase 实时数据库中输入为 BS 的“类别”的数据可以显示在业务类别页面下。
我可以在类的主体中放入条件表达式,还是应该将其放置在 Widget 中?
class _BusinessPage1State extends State<BusinessPage1> {
List<AllCourses> coursesList = [];
@override
void initState(){
super.initState();
DatabaseReference referenceAllCourses = FirebaseDatabase.instance.reference().child('AllCourses');
referenceAllCourses.once().then(((DataSnapshot dataSnapshot){
coursesList.clear();
var keys = dataSnapshot.value.keys;
var values = dataSnapshot.value;
for(var key in keys){
AllCourses allCourses = new AllCourses(
values [key]["courseName"],
values [key]["teacher"],
values [key]["category"],
);
coursesList.add(allCourses);
}
setState(() {
//
});
}));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.white),
onPressed: ()
{Navigator.pop(context);
Navigator.push(context, MaterialPageRoute(builder: (context)=>frontpage()));}),
title: Text("Creator's Club"),
backgroundColor: Color(0xff2657ce),
elevation: 0,),
body: coursesList.length == 0 ? Center(child: Text("No Data Avail", style: TextStyle(fontSize: 15),)): ListView.builder(
itemCount: coursesList.length,
itemBuilder: (_,index){
return CardUI(coursesList[index].courseName, coursesList[index].teacher, coursesList[index].category);}
)
);
}
}
Widget CardUI (String courseName, String teacher, String category){
return Card(
elevation: 7,
margin: EdgeInsets.all(15),
color: Colors.grey,
child: Container(
color: Colors.white,
margin: EdgeInsets.all(1.5),
padding: EdgeInsets.all(10),
child: Column(
children: <Widget>[
Text(courseName, style: TextStyle(fontSize: 10)),
SizedBox(height: 5),
Text(teacher, style: TextStyle(fontSize: 10)),
SizedBox(height: 5),
Text(category, style: TextStyle(fontSize: 10)),
SizedBox(height: 5),
],
)
),
);
}
【问题讨论】:
标签: firebase flutter firebase-realtime-database conditional-operator