I have difficulties on how to group sets of flutter widgets based on data returned from server.
The sample data is as below:
{
id: 1,
group: 1,
name: A,
},
{
id: 2,
group: 1,
name: B,
},
{
id: 3,
group: 2,
name: C,
},
{
id: 3,
group: null,
name: D,
}
Lets say those group that not null will group into a set of Dropdownlist while others are checkboxed, I have been trying so many hours by still cant and I am new to Flutter.
Is that a List? You can group by filtering with where, i.e., data.where((x) => x["group"] != null).
Full Dart code, see dartpad.dev:
void main() {
var data = [
{
"id": 1,
"group": 1,
"name": "A",
},
{
"id": 2,
"group": 1,
"name": "B",
},
{
"id": 3,
"group": 2,
"name": "C",
},
{
"id": 3,
"group": null,
"name": "D",
}
];
print(data.where((x) => x["group"] != null));
}
Related
I'm very new to mongodb. I'm trying to do an aggregation pipeline for lookup kinda like SQL left join. Given the following document's schema:
Mongo playground: https://mongoplayground.net/p/yAIwH5V2yv8
Characters:
{
"_id": 1,
"account_id": 1,
"world_id": 0,
"name": "hello"
}
Inventories:
{
"_id": 7,
"character_id": 2,
"type": "EQUIPPED"
}
Items:
{
"_id": 1,
"inventory_id": 7
}
I want to query for characters and look up inventories as well as items in inventories. I was able to achieve this however I would like to separate the inventories field in characters result document.
Current result:
{
"_id": 2,
"account_id": 1,
"world_id": 0,
"name": "hello",
"inventories: [
{
"_id": 1,
"character_id": "2",
"type": "EQUIPPED",
"items: [...]
}
]
}
What I want is based on the type of inventory I want it to be a separate field of the resulted character document something like this:
{
"_id": 2,
"account_id": 1,
"world_id": 0,
"name": "hello",
"equippedInventory: {
"_id": 1,
"character_id": "2",
"type": "EQUIPPED",
"items: [...]
},
"equipInventory: {
"_id": 2,
"character_id": "2",
"type": "EQUIP",
"items: [...]
},
}
Also, is my pipeline the best way to achieve this?
I have a list of data array from an API and I need to check the id with another hardcoded data array id.And I need to add two data elements which I'm getting from the api data array to the hardcoded array.
This is the hardcoded data array.
List<Data> data = [
Data(id: 1, title: 'title 1', comment: null, selected: null),
Data(id: 2, title: 'title 2', comment: null, selected: null),
Data(id: 3, title: 'title 3', val: null, comment: null, selected: null),
];
This is the data response from the API.
"data": [
{
"id": 1,
"value": "value 1",
"comment": "comment 1",
"userId": 1,
},
{
"id": 2,
"value": "value 2",
"comment": "comment 2",
"userId": 2,
},
{
"id": 3,
"value": "value 3",
"comment": "comment 3",
"userId": 3,
},
]
What I wanna do is the value I'm getting for comment and value from this response data list to add to the hardcoded array comment and selected.They're initially null.
Consider a dynamic list
List<dynamic> data = [
{ 'id': 1, 'title': 'title 1', 'comment': null, 'selected': null },
{ 'id': 2, 'title': 'title 2', 'comment': null, 'selected': null },
{ 'id': 3, 'title': 'title 3', 'val': null, 'comment': null, 'selected': null},
];
and the response from api is
List<dynamic> apiResp = [
{
"id": 1,
"value": "value 1",
"comment": "comment 1",
"userId": 1,
},
{
"id": 2,
"value": "value 2",
"comment": "comment 2",
"userId": 2,
},
{
"id": 3,
"value": "value 3",
"comment": "comment 3",
"userId": 3,
},
];
to update the comment and selected fields from api response you can do something like this
for (var each in data) {
var index = apiResp.indexWhere((element) => element!['id']!.toString() == each!['id']!.toString());
if (index > -1) {
data[index]!['comment'] = apiResp[index]!['comment'];
data[index]!['selected'] = apiResp[index]!['selected'];
}
}
if you want to make sure your data is updated just print it like this
// print to ensure the data is update
for (var each in data) {
print(each);
}
Note: I added .toString with id incase if you are not sure about data type and also if your response from api is not in list form you can convert it,
var data = json.decode(utf8.decode(response.bodyBytes));
return data.toList();
I am new in Dart and I have been trying to access the value of "farm_size" key in the following piece of code but unsuccessful. I can access up to "farms" like this
print(myData1[0]["farms"]);
But not any further. Any help will be appreciated. Thanks.
void main() {
var myData1 = [
{
"id": 1,
"first_name": "Jordan",
"sur_name": "Zamunda",
"member_n0": "AA1",
"gender": "male",
"phone": "123456789",
"email": "jordan#example.com",
"farms": [
{"id": 1, "farm_name": "SOUTH_FARM", "farm_size": "160 acres"},
{"id": 2, "farm_name": "NORTH_FARM", "farm_size": "190 acres"}
]
}
];
print(myData1[0]["farms"]);
}
inside farms, there is a list of more maps, to access those maps, you need to specify the index of the map you, for example we want to get the first map, we type:
print(myData1[0]["farms"][0]);
//this will print => {"id": 1, "farm_name": "SOUTH_FARM", "farm_size": "160 acres"}
//to get the name of this farm, you use the key 'farm_name' now:
print(myData1[0]["farms"][0]['farm_name']);
//prints=> SOUTH_FARM
If you're running it in dartpad, this will work:
void main() {
var myData1 = [
{
"id": 1,
"first_name": "Jordan",
"sur_name": "Zamunda",
"member_n0": "AA1",
"gender": "male",
"phone": "123456789",
"email": "jordan#example.com",
"farms": [
{"id": 1, "farm_name": "SOUTH_FARM", "farm_size": "160 acres"},
{"id": 2, "farm_name": "NORTH_FARM", "farm_size": "190 acres"}
]
}
];
List<Map<String,dynamic>> _list = myData1[0]["farms"]as List<Map<String,dynamic>> ;
print(_list[0]['farm_name']);
}
try this
List m = myData1[0]['farms'] as List;
print(m[0]['id']);
I have some JSON data that I am getting from my database. I can pull it fine and load it into my table view. my issue is separating my JSON data so I can section the tableview. I have JSON array like this
"data": [
{
"ID": 1,
"wilayah_id": 1,
"name": "Jembatan Lima",
"region_name": "region1"
},
{
"ID": 2,
"wilayah_id": 1,
"name": "Kebon Jeruk",
"region_name": "region1"
},
{
"ID": 18,
"wilayah_id": 3,
"name": "Waylunik",
"region_name": "region2"
},
{
"ID": 19,
"wilayah_id": 3,
"name": "Tenggiri",
"region_name": "region2"
},
{
"ID": 25,
"wilayah_id": 3,
"name": "Mesuji",
"region_name": "region3"
},
{
"ID": 26,
"wilayah_id": 4,
"name": "KM 6",
"region_name": "region3"
}
]
What I'm trying to do is separate this data into three parts sort by "region_name" in my table view.
//region model
struct RegionList {
var ID:String
var wilayah_id:String
var name:String
var region_name:String
static var dataSource:[RegionList] {
return [
RegionList.init(ID: "1", wilayah_id: "1", name: "3", region_name: "R1"),
RegionList.init(ID: "1", wilayah_id: "1", name: "3", region_name: "R2"),
RegionList.init(ID: "1", wilayah_id: "1", name: "3", region_name: "R1"),
RegionList.init(ID: "1", wilayah_id: "1", name: "3", region_name: "R2"),
RegionList.init(ID: "1", wilayah_id: "1", name: "3", region_name: "R3")
]
}
}
//Coming Json Array
var arrRegion:[RegionList] = []
//MAKe Section Array by Using Model Class
var regionInSectionArray:[[RegionList]] = []
//Using Dic key in String and value is Model
var usingDicKeySectionArray:[String:[RegionList]] = [:]
// Dummy Data
arrRegion = RegionList.dataSource
//Using Default Dictionary Method to make a group
usingDicKeySectionArray = Dictionary.init(grouping: arrRegion, by: { (region) -> String in
/// Here we are making group dictionary by using region name, you can set by own requirment
//Specify by Region
return region.region_name
})
// You can use it in number of section usingDicKeySectionArray.count
//And number of rowInSection usingDicKeySectionArray[section].count
print(usingDicKeySectionArray.map{$0.key})
//end
///Sorting Keys
//Second option
let sortedKeys = usingDicKeySectionArray.keys.sorted()
sortedKeys.forEach { (key) in
//
let region = usingDicKeySectionArray[key]
regionInSectionArray.append(region ?? [])
}
//USe
//regionInSectionArray.count // in numberOfSection
//regionInSectionArray[section].count // in numberOfRowInSection
//result = ["R1", "R2", "R3"]
I'm trying to get the GitHub data using Talend big data. The thing is, i have multiple URLs,because used each URL to take some values & stored into single mongoDB. The below order only i'm going to try & get the informations,
https://api.github.com/users/sample/repos
https://api.github.com/repos/sample/awesome-ciandcd/commits
https://api.github.com/repos/sample/awesome-ciandcd/contributors
Each URLs are giving the single JSONArray with multiple data format.Please give some suggestion to do this. I've already tried with sub-jobs component. But not get clear job.
My Output Should be like,
{
"gitname": "sample",
"gitType" : "User",
"password": "password",
"repoCount": 3,
"repositoryDetails": [
{
"repoName": "MergeCheckRepository",
"fileCount": 10,
"branchCount": 6,
"releaseCount": 2,
"commitsCount": 10,
"contributorsCount": 3,
"totalPulls": 1,
"mergeCount": 1,
"totalIssues": 12,
"closedIssueCount": 3,
"watchersCount": 1,
"stargazersCount": 4,
"contributorsDetails": [
{
"login": "sample",
"avatarURL": "https://avatars2.githubusercontent.com/u/30261572?v=4",
"contributions": 3
}
],
"commitDetails": [
{
"name": "sample",
"email": "sampletest#test.com",
"date": "2017-07-20T09:09:09Z"
}
]
},
{
"repoName": "Dashboard",
"filecount": 19,
"branchCount": 4,
"releasecount": 2,
"commitsCount": 5,
"contributorsCount": 3,
"totalPulls": 1,
"totalIssues": 2,
"closedIssueCount": 3,
"watchersCount": 1,
"stargazersCount": 4,
"contributorsDetails": [
{
"login": "sample",
"avatarURL": "https://avatars2.githubusercontent.com/u/30261572?v=4",
"contributions": 3
},
{
"login": "Dashboard",
"avatarURL": "https://avatars2.githubusercontent.com/u/30261572?v=4",
"contributions": 3
}
],
"commitDetails": [
{
"name": "sample",
"email": "sampletest#test.com",
"date": "2017-07-14T09:09:09Z"
},
{
"name": "Dashboard",
"email": "prakash.thangasamy#test.com",
"date": "2017-07-19T09:09:09Z"
},
{
"name": "testrepo",
"email": "test.dashboard#test.com",
"date": "2017-07-20T09:09:09Z"
}
]
}
]
}
How to achieve this one with sub-job? Is there any other way to do this?