代码之家  ›  专栏  ›  技术社区  ›  john Gamey

如何将状态从数字转换为文本

  •  0
  • john Gamey  · 技术社区  · 2 年前

    我将状态存储在一个表中,并将其保存为数字0和1。我希望在列表视图生成器中显示这些状态。 但我希望这些状态显示为文本而不是数字。 0=待批准 1=批准 我如何开始构建以获得该结果?我刚开始写颤振。

    class _view_problemState extends State<view_problem> {
      TextEditingController _searchController = TextEditingController();
    
      List<problemModel> problemlist = [];
      List<problemModel> originalList = [];
      StreamController _streamController = StreamController();
       Future getAllProblem() async {
        problemlist = await problemcontrollers().getProblem();
        originalList = problemlist;
        _streamController.sink.add(problemlist);
      }
    
      @override
      void initState() {
        getAllProblem();
        super.initState();
      }
      @override
       Widget build(BuildContext context) {
         return Scaffold(
           appBar: AppBar(
             backgroundColor: Color.fromARGB(255, 14, 12, 134),
            title: const Text('show information'),
          ),
          body: Container(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(8.0),
                 )
                 Expanded(
                   child: StreamBuilder(
                     stream: _streamController.stream,
                    builder: (context, snapshots) {
                      if (snapshots.hasData) {
                        return ListView.builder(
                            itemCount: problemlist.length,
                            itemBuilder: ((context, index) {
                              problemModel problem = problemlist[index];
                               return Card(
                                 margin: EdgeInsets.all(10),
                                 child: ListTile(
                                   leading: CircleAvatar(
                                     backgroundImage: NetworkImage(
                                      'http://192.168.1.5/skc/Problem_image/${problem.image}',
                                    ),
                                  ),
                                  title: Text(
                                    problem.name_surname,
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold, fontSize: 19),
                                  ),
                                  subtitle: Text(
                                    problem.status,
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold, fontSize: 17),
                                  ),
                                ),
                              );
                            }));
                       }
                      return Center(
                        child: CircularProgressIndicator(),
                      );
                    },
                  ),
                )
              ],
            ),
          ),
        );
      }
    }
    
    0 回复  |  直到 2 年前
        1
  •  0
  •   Rifat Hossain    2 年前

    在这种情况下,我们可以使用三元运算符。 就这样更新你的字幕吧

    Text(
      problem.status == 0 ? 'pending approval' : 'approve',
      style: TextStyle(
          fontWeight: FontWeight.bold, fontSize: 17),
    )