skip to Main Content

I store user status information in the database but in numbers. I want to show it in listview builder but display it as text instead of numbers.The

number 0 shows an unauthorized message.

Number 1 shows allowed messages.

Number 2 shows the initial message.

How do I start building to get that result? I just started writing flutter.

class _view_problemState extends State<view_problem> {
 
  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}',
                                ),
                              ), 
                              subtitle: Text(
                                problem.status,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 17),
                              ),
                            ),
                          );
                        }));
                   }
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}

2

Answers


  1. This should do the trick. If it works for you, Remember to Accept the Answer and UpVote

    class _view_problemState extends State<view_problem> {
     
      List<problemModel> problemlist = [];
      List<problemModel> originalList = [];
      StreamController _streamController = StreamController();
       Future getAllProblem() async {
        problemlist = await problemcontrollers().getProblem();
        originalList = problemlist;
        _streamController.sink.add(problemlist);
      }
      
      String getStatusText(int statusCode) {
        switch (statusCode) {
          case 0:
            return "Unauthorized";
          case 1:
            return "Authorized";
          case 2:
            return "Initial";
          default:
            return "UnRecognised"; 
        }
      }
      @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}',
                                  ),
                                ),
                                subtitle: Text(
                                  getStatusText(problem.status),
                                  style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    fontSize: 17,
                                  ),
                                ),
                              ),
                            );
                          }),
                        );
                      }
                      return Center(
                        child: CircularProgressIndicator(),
                      );
                    },
                  ),
                )
              ],
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. Raky’s answer seem logical and should work properly, have you try cast your problem.status to String before sending it to the getStatusText() method?

    Here is the casting example:

    subtitle: Text(
        getStatusText(problem.status.toString()),
        style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 17,
        ),
    ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search