Home > Software design >  How can I get the number of "read" = "true" in the following code
How can I get the number of "read" = "true" in the following code

Time:09-29

Here I am getting the response like the following in the postman

    [
        {
            "createdAt": "2022-09-19T18:43:27.404Z",
            "title": "title 1",
            "description": "description 1",
            "lastdate": "lastdate 1",
            "status": "status 1",
            "id": "1",
            "read": "false"
        },
        {
            "createdAt": "2022-09-20T04:01:37.296Z",
            "title": "title 2",
            "description": "description 2",
            "lastdate": "lastdate 2",
            "status": "status 2",
            "id": "2",
            "read": "false"
    
        }
    ]
    

I want to get the number(Count) of read value == false in the code. So that I can set that count to one notification badge

  
  class NotificationView extends StatefulWidget {
    const NotificationView({Key? key}) : super(key: key);
  
    @override
    State<NotificationView> createState() => _NotificationViewState();
  }
  
  class _NotificationViewState extends State<NotificationView>
      with ChangeNotifier {
    NotificationViewModel? _notificationViewModel;
    final NotificationHelper _notificationHelper = NotificationHelper();
    ValueNotifier<int> notificationCounter =
        ValueNotifier(SideMenuView.notificationCount.value);
    ValueNotifier<int> unreadNotificationCount = ValueNotifier(0);
    @override
    void initState() {
      super.initState();
      _notificationHelper.initialiseNotifications();
      _notificationHelper.sendNotification('Hi', 'This is to test notifications');
      notificationCounter.value  ;
      SideMenuView.notificationCount.value = notificationCounter.value;
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
        _notificationViewModel =
            Provider.of<NotificationViewModel>(context, listen: false);
  
        _notificationViewModel!.getNotifications();
      });
    }
  
    @override
    void dispose() {
      super.dispose();
      notificationCounter.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      final sideMenuViewModel = Provider.of<SideMenuViewModel>(context);
      _notificationViewModel = Provider.of<NotificationViewModel>(context);
      Size size = MediaQuery.of(context).size;
      return Scaffold(
          body: ChangeNotifierProvider<NotificationViewModel>(
        create: (context) => NotificationViewModel(),
        child: Consumer<NotificationViewModel>(
            builder: (context, notificationViewModel, child) {
          return ListView.builder(
              itemCount: _notificationViewModel!.notificationModel.length,
              itemBuilder: (BuildContext context, int index) {
                return InkWell(
                  onTap: () {
                    sideMenuViewModel.setRouteName(RoutesName.inspections);
                  },
                  child: SizedBox(
                    child: Card(
                      margin: const EdgeInsets.fromLTRB(20, 5, 20, 15),
                      color: AppColors.veryLightGrey,
                      elevation: 10,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10)),
                      child: ConstrainedBox(
                        constraints: const BoxConstraints(minHeight: 120.0),
                        child: Container(
                          margin: const EdgeInsets.fromLTRB(5, 5, 15, 15),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              SizedBox(height: size.height * 0.01),
                              Text(
                                _notificationViewModel!
                                    .notificationModel[index].read!,
                                style: const TextStyle(
                                    fontSize: 18.0,
                                    fontFamily: "Helvetica",
                                    fontWeight: FontWeight.bold),
                              ),
                              SizedBox(height: size.height * 0.02),
                              Text(
                                _notificationViewModel!
                                    .notificationModel[index].description!,
                                style: const TextStyle(
                                  fontSize: 14.0,
                                  fontFamily: "Helvetica",
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                );
              });
        }),
      ));
    }
  }

CodePudding user response:

You can get count like this.

int count =0;

_notificationViewModel!.notificationModel.forEach((element){
  if(element.read == false){  
   count =count 1;
  }
});

CodePudding user response:

if read comes with different type data such "read":false and "read":3 then, you can check runtimeType,

int readCount = 0;
if(json["read"] is int){
  readCount = json["read"];
}
  • Related