Flutter : can't get all response body from get request - flutter

I have get request in Flutter app, when I test the request in postman I get all data, something like this :
{
"result":{
"name" : "somename",
"images":[
"test.jpg",
"test2.jpg"
],
"sizes":[
{
"id": 1,
"value" : 5
},
{
"id": 2,
"value" : 15
}
]
}
}
I call data and print them like this without using models:
var data = json.decode(response.body);
print(data['result']['name']);
print(data['result']['images']);
print(data['result']['sizes']);
it is print all things expect last one.
where must be the mistake?

Solved, by adding "?sizesView = true" to the link
final response = await http.get(path +'?sizesView = true'):

you should get the index of the last one because it is in a dictionary not a list do this:
print(data['result']['sizes'][0]['id']) // it will get the first index of the sizes list and then get the id key
or you can creat a model of list to get the indexes of your list

Related

how to add multiple fileds of the same name with different data

so what i am doing is sending this data to the the firebase firestore here's my code its working fine,
final data = {
"sent_requests" : {
"sender" : "${loggedInUser.uid}",
"receiver" : args.uid,
"details" : {
"date" : detail_class.datetime,
"total_dishes" : detail_class.total_dishes,
"total_people" : detail_class.total_people,
"meals" : detail_class.meals,
"location" : detail_class.location,
}
}
};
dynamic db = FirebaseFirestore.instance;
db.collection("users").doc("${loggedInUser.uid}").set(data, SetOptions(merge: true));
This result is fine, but this code just overwrites the previous request. I instead want to add more requests in it,
when the sender and receiver are different.
Field names are unique in Firestore, so you can't have (for example) two receiver fields.
What you can have is a single field that is an array of values. I'd typically call that receivers (plural) to indicate that it's a multi-value field, and you'd write that from your code with:
final data = {
"sent_requests" : {
...
"receivers" : [args.uid],
...
}
};
This sets the receivers field to an array with just the value or args.uid. If you want to merge the args.uid with any existing values in the database already, you can use an array-union operator:
final data = {
"sent_requests" : {
...
"receivers" : FieldValue.arrayUnion([args.uid]),
...
}
};
Now the args.uid value will be added to the receivers array, unless it's already in there.

A Map related error in flutter which happens everytime I try to parse it

I have a map which contains two standard key and value pairs like
_body = {
"expected_time": "$Time",
"payment": "$payment",
"receiver" : {},
};
And another map named receiver inside it as you can see. Values are being passed to the receiver later using a for loop and the information is being added just fine.
for(int i=0; i<=n; i++)
{
_body['receiver'][i] = {
"receiver_name" : "abc",
};
}
The issue I'm facing is when trying to send this map to an api call via http.post
there jsonEncode(body) has been used to encode the map to send it. When sending those simple key value pairs I'm getting no error but when I'm trying to include the receiver field as well I'm getting the error.
Can anyone please tell me what I need to here? Thanks!
you are not making it in the right way, try this
var _body = {
"expected_time": "time",
"payment": "payment",
"receiver" : {},
};
for(int i=0; i<=3; i++) {
_body.addAll({
'receiver[$i]': {
"receiver_name": "abc",
}
});
}
print(_body);
and the output is like this
{expected_time: time, payment: payment, receiver: {}, receiver[0]: {receiver_name: abc}, receiver[1]: {receiver_name: abc}, receiver[2]: {receiver_name: abc}, receiver[3]: {receiver_name: abc}}
you can now encode it

changed object structure from item with object to array of objects in MongoDB shows error "An error occurred while deserializing"

I have changed the object structure from
{
id:"...",
name:"...",
url:"...",
studentAccess:{
"A":"....",
"B":"...",
},
ecosystemId:"..."
}
TO:
{
id:"...",
name:"...",
url:"...",
studentAccess:[
{
"X":"....",
"Y":"..."
},
{
"X":"....",
"Y":"..."
},
{
"X":"....",
"Y":"..."
},
],
ecosystemId:"..."
}
in API we get list of these objects based on ecosystemid or student access item "X", name or any field..calling like this in API
var acc = await _eco.FindByUser();
var query = await _db.CourseDocuments.FindAsync(w => w.EcosystemId == acc.Identifier);
after query executes i get this error pls help me i need to change structure anywhere after it changed in mongoDB?

What am I doing wrong when manipulating data in Meteor/MongoDB?

I have this helper
myClub: function(){
var currentUserId = Meteor.userId();
var user = Meteor.users.findOne({_id: currentUserId});
return user;
}
I want it to return user.role
Here is my user in MongoDB
{
"_id" : "RdirmrLG3t8qBk4js",
"createdAt" : ISODate("2016-04-17T19:40:56.877Z"),
"services" : {
"password" : {
"bcrypt" : "$2a$10$cPe92XR9DT238bH/RanYEu.J6K2ImvAEbWOcVq6j9luI0BH08Qdly"
},
"resume" : {
"loginTokens" : [
{
"when" : ISODate("2016-04-17T19:51:49.474Z"),
"hashedToken" : "uVKUj/7JEkkOuizXhjl212Z38E47HXCex+D4zRikQ1k="
}
]
}
},
"username" : "worker",
"role" : "worker",
"club" : "hzSKAJfPXo7hSpTYS"
}
The code above works just fine. So it finds the current user and outputs info about it. But when I change user to user.role I get the following errormessage.
TypeError: Cannot read property 'role' of undefined
at Object.myClub
How can it be undefined? Is my syntax incorrect?
Template helpers are reactive, which means they update themselves as the app state changes or new data appears. In your case, the helper is called immediately when the template is rendered and before the Meteor.users collection is filled. Therefore, the .findOne() method returns undefined. It will be corrected in the second pass after new data arrives.
The simple fix here is to check whether the data is present inside the helper:
myClub: function(){
var currenUserId = Meteor.userId();
var user = Meteor.users.findOne({_id: currenUserId});
if(!user) return 'NO DATA';
return user.role;
},
In real life you'll probably want to wait for the basic data to be loaded before you render the template. That is usually done on the controller level.
Try:
myClub: function(){
return Meteor.user() && Meteor.user().role;
}
This is shorthand for return the role if there's a user.
As far as the role field not showing up, make sure that you are publishing that key from the server and subscribing to it. For example:
Meteor.publish('me',function(){
return Meteor.users.find(this.userId,{fields: {role: 1, username: 1, profile: 1, emails: 1}});
});
And on the client:
var me = Meteor.subscribe('me');
if ( me.ready() ) console.log("Ta-da! The role is: "+Meteor.user().role);
make sure that you subscribed to all data you need.
By the way, you can try following:
role: function(){ return (Meteor.user() || {}).role; }
Cheers

How to find payment collection using response id?

I am developing angularjs nodejs application
Following has Payment Collection find function and result
var collectionId = "5673d6c7da28e94f51277894"
Payment.find({id: collectionId}).exec(function(err,payment)
console.log(payment);
);
Console result :
{
"_id" : ObjectId("5673d6c7da28e94f51277894"),
"response" : {
"status" : "approved",
"id" : "PAY-9N740711P28316116KZX5U4I"
}
}
I need to find payment collection using response id
My code here
var paymentId = "PAY-9N740711P28316116KZX5U4I"
Payment.find({ response : {id: paymentId}}).exec(function(err,payment)
console.log(payment);
);
Console result :
undefined
If you are not clear question, please comment
Hope answer, thanks
It should be:
Payment.find({ 'response.id': paymentId }).exec(function(err,payment) {
console.log(payment);
});