0

I'm not sure if my strategy or logic is correct to achieve this, but I have 2 lists coming from 2 different jsondata mysql queries, let's say they look like this:

List newsAgency = [{id: 1, name: 9news, image: x} 
                   {id: 2, name: abcnews, image: y} 
                   {id: 3, name: bbcnews, image:z}];

List following = [{userid: 41, username: x, newsid: 2}
                  {userid: 41, username: x newsid: 3}];

I want to see if the id in newsAgency matches the newsid in the following list, and to return true or false correspondingly.

The idea is to say that I am following 2 news agencies out of 3, so my goal is to display the button to follow or unfollow based on the results.

I tried everything suggested from this post how can I find a list contains in any element another list in flutter? but couldn't get it.

This is my code example:

Listview.builder(
  itemCount: newsAgency.length,
  itemBuilder: (BuildContext context, int index) {
    bool following = false;
    return Card(
        elevation: 10.0,
        child: Row(
            children: [
                Text(newsAgency[index]['name'],
                following 
                ? TextButton(onPressed: () {//unfollow function},
                            child: const Text('unfollow')),
                : TextButton(onPressed: () {//follow function},
                            child: const Text('follow')),
                ]));});

Any help is highly appreciated

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Texv
  • 1,225
  • 10
  • 14
  • Hi, the newsAgency has one element which is a Map, they are not separated ? – Gwhyyy Nov 17 '22 at 14:54
  • the keys in the map are equal since they is same keys in same map – Gwhyyy Nov 17 '22 at 14:56
  • can you clarify more what you want to achieve – Gwhyyy Nov 17 '22 at 14:56
  • not sure what you mean, I want to see if an item inside newsagency list array contains a value that equals to an item inside following list array – Texv Nov 17 '22 at 14:59
  • yes, I understand that,so you get the idea, in your example of newsAgency list, tell me what this should print newsAgency[0]['id'] – Gwhyyy Nov 17 '22 at 15:01
  • I tried writing an algorithm, but the list items are not separated, they all collected under one map – Gwhyyy Nov 17 '22 at 15:03
  • newsAgency[0]['id'] should print "1", and i want to see if that "1" result equals to any newsid value in followinglist. then return true or false for each item in newsagency – Texv Nov 17 '22 at 15:05
  • I mean, if you know a whole new strategy to achieve the result im looking for im happy with that. I just want to return true or false on whether or not the user is following each item in the newsagency list – Texv Nov 17 '22 at 15:10
  • I'm can help but I guess you didn't get my idea, please tell me what this should print newsAgency[2]['id'] – Gwhyyy Nov 17 '22 at 15:14
  • your newsAgency list doesn't have multiple items for agencies, it have only one item which is a map holding all your data, they are not separated – Gwhyyy Nov 17 '22 at 15:15
  • to do a proper filter of what is following and for what's not, each agency should be a single separated map from others so we can search its id in the following list of items – Gwhyyy Nov 17 '22 at 15:16
  • newsAgency[2]['id'] prints 3, is there a way to separate them? thats how data is received from jsondata – Texv Nov 17 '22 at 15:17
  • no, it will not print 3, please try printing it on dartpad.dev – Gwhyyy Nov 17 '22 at 15:20
  • ohhh wait i see what you mean, sorry i wrote this wrong, ill edit the question – Texv Nov 17 '22 at 15:21
  • Fixed, sorry about that – Texv Nov 17 '22 at 15:22
  • try the code in my answer – Gwhyyy Nov 17 '22 at 15:39

2 Answers2

1

add this method in your class, it will be responsible for searching which items are following and which are not, and return a bool based on it:

  bool checkIsFollowing(Map<String, dynamic> current) {
    for(int index = 0; index < following.length; index+=1) {
      if(current["id"] == following[index]["newsid"]) {
        return true;
      }
    }
    return false;
  }

now inside of your ListView's itemBuilder, replace this:

    bool following = false;

with this:

 final currentNewsAgency = newsAgency[index];
 bool following = checkIsFollowing(currentNewsAgency);
 

following will be true or false based on if the current Agency's id exists in some item in the following list.

Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
0

use contains() method to check if the list contains the value you want to match or not, for example:-

following.contains(newsAgency[index]['name'].toString())// true if value matches.

Pawan
  • 31
  • 9
  • I tried that before and just now again but it shows result as false when it should be true. Thats because newsAgency[index]['name'].toString() returns the number 5, so its doing following.contains(5) – Texv Nov 17 '22 at 15:59
  • also im trying to match the id not name, those names are different, but thanks :) – Texv Nov 17 '22 at 16:05