【问题标题】:How to add a condition to display certain data?如何添加条件来显示某些数据?
【发布时间】: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


    【解决方案1】:

    如果我理解正确(在构建方法中):

    final businessList = coursesList.where((item) => item.category != null).toList();
    
    ...
    ListView.builder(
      itemCount: businessList.length,
      itemBuilder: (_, index) {
        final item = businessList[index];
        return CardUI(item.courseName, item.teacher, item.category);
        
      }
    );
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-30
      • 2012-11-12
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多