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

huangapple go评论144阅读模式
英文:

How to convert status from number to text

问题

Sure, I'll help you modify your Flutter code to display the states as text instead of numbers. Here's the updated code:

class _view_problemState extends State<view_problem> {
  TextEditingController _searchController = TextEditingController();

  List<problemModel> problemlist = [];
  List<problemModel> originalList = [];
  StreamController _streamController = StreamController();

  // Define a map to map numbers to their corresponding text values
  Map<int, String> statusMap = {
    0: "pending approval",
    1: "approve",
  };

  Future getAllProblem() async {
    problemlist = await problemcontrollers().getProblem();
    originalList = problemlist;

    // Map the status numbers to text values
    for (var problem in problemlist) {
      problem.status = statusMap[problem.status] ?? "Unknown";
    }

    _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(),
                  );
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}

I've added a statusMap that maps the status numbers to their corresponding text values. In the getAllProblem function, I've updated the problem.status to use the text value based on the mapping. This will display the statuses as text in the ListView.

英文:

I'm storing the states in a table which I keep as numbers 0 and 1. I want to get these states displayed in the listview builder.
But I want these statuses to be displayed as text instead of numbers.
0 = pending approval
1 = approve
How do I start building to get that result? I just started writing flutter.

class _view_problemState extends State&lt;view_problem&gt; {
TextEditingController _searchController = TextEditingController();
List&lt;problemModel&gt; problemlist = [];
List&lt;problemModel&gt; 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(&#39;show information&#39;),
),
body: Container(
child: Column(
children: &lt;Widget&gt;[
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(
&#39;http://192.168.1.5/skc/Problem_image/${problem.image}&#39;,
),
),
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(),
);
},
),
)
],
),
),
);
}
}

答案1

得分: 0

我们可以在这种情况下使用三元运算符。
只需像这样更新您的字幕:

Text(
problem.status == 0 ? '待批准' : '已批准',
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 17),
)
英文:

We can use ternary operator in this case.
Just update your subtitle like that

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

huangapple
  • 本文由 发表于 2023年8月10日 20:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定