Dart/Flutter null check on a map object - flutter

I am building a map from a json response, one of the itmes in the map is as follows
isbn: (jsonBook['volumeInfo']['industryIdentifiers']?.length < 1 ? 'NA' : jsonBook['volumeInfo']['industryIdentifiers'][0]['identifier'] ?? 'NA'),
These are following conditions I am dealing with
The api response for some items may not have industryIdentifiers, when I am checking for null using ? it gives me error saying < was called on null, which is a possibility, how do I check that without breaking the rest of the checks (which is working fine)
To sum it up, I want to check if "industryIdentifiers" is a null, if yes return 'NA' if not null then check if any nested items "identifer" is null if yes return 'NA' else return the value for identifier.
This is the structure of JSON response:
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "1484799410"
},
{
"type": "ISBN_13",
"identifier": "9781484799413"
}
],

Related

i try to get data from api but when there is null there, after first null i get null after it

there is null there, after first null I get nulls after it, cause it got caught but exception, how to make exception?enter image description here. As you can see in picture, vehichles and starship are null, and caught by exeption, after them there is 3 data, they become null too. When i remove vehichles and starship everything is ok
How to make it ? I wanna get null for vehichles and starship and get data normally after them(Soryy for my English)
Future<dynamic> getSwData() async{
return await MyStarWarsService().getDataFromSWApi().then((value) async{
if(value != null){
setState((){
name = value!.name!;
height = value!.height!;
mass = value!.mass!;
birth_year = value!.birth_year!;
eye_color = value!.eye_color!;
films = value!.films!;
gender = value!.gender!;
hair_color = value!.hair_color!;
homeworld = value!.homeworld!;
skin_color = value!.skin_color!;
vehicles = value.vehcles; //// it is NULL
starships = value.starsips;// it is NUll
species = value!.species!;/// NOT null
created = value!.created!;//// NOT null but come NUll
edited = value!.edited!;//// NOT null but come NULL
url = value!.url!;//// NOT null, but come NULL
});
return value;
}
},
).catchError((_){
throw Exception("Exception is caught"); this exception caught null and after it everithings become null
});
}
}
HTTP 200 OK
Content-Type: application/json
Vary: Accept
Allow: GET, HEAD, OPTIONS
{
"name": "C-3PO",
"height": "167",
"mass": "75",
"hair_color": "n/a",
"skin_color": "gold",
"eye_color": "yellow",
"birth_year": "112BBY",
"gender": "n/a",
"homeworld": "https://swapi.dev/api/planets/1/",
"films": [
"https://swapi.dev/api/films/1/",
"https://swapi.dev/api/films/2/",
"https://swapi.dev/api/films/3/",
"https://swapi.dev/api/films/4/",
"https://swapi.dev/api/films/5/",
"https://swapi.dev/api/films/6/"
],
"species": [
"https://swapi.dev/api/species/2/"
],
"vehicles": [], it null
"starships": [], it is null
"created": "2014-12-10T15:10:51.357000Z", afetr i get nulls for this but it is not null
"edited": "2014-12-20T21:17:50.309000Z",
"url": "https://swapi.dev/api/people/2/"
}
I am struggling to understand the question (poor English), but it seems like you want to accept null as the value if it I null. If that's true, using the ! after the value tells the code that you are sure there is a non null value. Change the ! to ? to allow null.
For example:
name = value!.name!;
becomes
name = value.name?;
Name can now have a null value.
I suggest you research null safety.
If you use async and await in your future and at the end SetState after Then, you will need to check the mounted in the tree like that :
Future<dynamic> getSwData() async{
return await MyStarWarsService().getDataFromSWApi().then((value){
if(this.mounted){
// add your SetState
}
}
}
This is a good habit to do.

Postgres query array elements inside JSON?

I have Postgres 11 table called fb_designs that has a json column with data structured like so:
{
"listings": [
{
"id": "KTyneMdrAhAEKyC9Aylf",
"active": true
},
{
"id": "ZcjK9M4tuwhWWdK8WcfX",
"active": false
}
]
}
and a tags column in a character varying[] format as so {dWLaRWChaThFPH6b3BpA,BrYiPaUiou020hsmRugR}. Both lengths are undefined.
What I am trying to do is produce a some queries that will let me say in laymans terms,
show me all results where at all items.listings has an
active status and tags contains BrYiPaUiou020hsmRugR
I got this far, however I'm not sure how to add in WHERE uid = "foo", WHERE tags contains "foo", "bar and WHERE title is like %hoot%
SELECT id, title, tags, selected_preview_image, items
FROM fb_designs r, json_array_elements(r.items#>'{listings}') obj
WHERE obj->>'active' = 'true'
GROUP BY id
If they are all true, then none of them are false. Sounds like you want to negate the containment operation over false.
select * from fb_designs where
not items::jsonb #> '{"listings":[{"active": false}]}'
and tags && ARRAY['BrYiPaUiou020hsmRugR']::varchar[]

Getting Cannot read property in postman scripts when null found

I am getting an Error when running a script in the Postman Tests tab, When trying to check that a property is not null.
My JSON response:
{
"statusMessage": "Success",
"timeStamp": "2018-01-23 05:13:16.7",
"numberOfRecords": 7,
"parties": [
{
"shippingAddress": null,
"shippingDetails": null,
"paExpirationDate": "",
"historyDate": "01/22/2018",
"renewal": {
"renewalRx": "N",
"priorRxNumber": "",
"priorSB": "",
"priorFillNumber": ""
},
"noOfRefillingRemaining": "1",
"ndc": "00074-3799-06",
"rxId": "7004942",
"fillingNumber": "0"
},
{
"shippingAddress": {
"addressLine1": "2150 St",
"addressLine2": "Home Line 2",
"city": "Bronx",
"state": "NY",
"zipCode": "10453",
"addressSeqNumber": "1",
"medFacilityIndicator": "N"
}
}
]
}
My postman script is:
var jsonData = JSON.parse(responseBody);
var parties = jsonData.parties;
parties.forEach(function(data){
if(data.shippingAddress!==null && data.shippingAddress.addressLine1 !== null ){
postman.setEnvironmentVariable("addressLine1",data.shippingAddress.addressLine1);
}
I am getting the following error:
"Error running tests for results: TypeError: Cannot read property 'addressLine1' of null"
You could try this, I changed your code slightly but this would work:
var parties = pm.response.json().parties
for(i = 0; i < parties.length; i++) {
if(parties[i].shippingAddress !== null) {
pm.environment.set("addressLine1", parties[i].shippingAddress.addressLine1)
}
}
I tested this locally with the Schema that you provided and that it wrote 2150 St to my environment file.
The schema you posted doesn't seem to be a complete one, I think that the parties array has a shippingAddress property which is either null or it is an object containing the shippingAddress details - I might be wrong but I can't get my head around the data that you posted.
I don't think what you're searching on the in the if statement is correct and it wouldn't work the way you have it because if the first condition is null (like in your response data) it wouldn't ever meet the second condition because the object wouldn't be there and that shippingAddress.addressLine1 reference would always show that error.
Or you could use your code like this:
var jsonData = JSON.parse(responseBody)
var parties = jsonData.parties
parties.forEach(function(data) {
if(data.shippingAddress !== null) {
postman.setEnvironmentVariable("addressLine1",data.shippingAddress.addressLine1)
}
})

How to match a wiremock POST request with some optional JSON parameters & any values?

I need to use wiremock to test a POST request that's sending data like this:
{
"name": "known fixed value",
"dateOfBirth": 5123456789000,
"email": "known fixed value",
"currentDate": any numeric value,
"status": any text value with alphabets, numbers and symbols
}
The 1st 3 fields, name, dateOfBirth and email are fixed, known values, that don't change from one request to the next.
The last 2 fields, currentDate and status change randomly from one request to the next, but are mandatory fields that can hold any value.
How do I design a mapping that tests this?
Thanks in advance.
You can use a JsonPath regex request body matcher, for example in your case you should use this JsonPath:
$[?(#.name == 'known fixed value' && #.dateOfBirth == 5123456789000 && #.email == 'known fixed value' && #.currentDate =~ /[0-9]*/i && #.status =~ /.*/i)]
Which will match an example request body:
{
"name": "known fixed value",
"dateOfBirth": 5123456789000,
"email": "known fixed value",
"currentDate": 23123,
"status": "rfjhg33443"
}
If you use JSON stubbing, you can write
"request": {
"bodyPatterns": [
{
"equalToJson": {
"name": "known fixed value",
"dateOfBirth": 5123456789000,
"email": "known fixed value",
"currentDate": "${json-unit.any-number}",
"status": "${json-unit.any-string}"
}
}
]
}
Ref: section "Placeholders" in http://wiremock.org/docs/request-matching/

datatype of complextype entity is null when returning an array of complex types

We have created a complextype field "carriers" which is an array of Carrier objects. See below metadata
"dataProperties": [
{
"name": "carriers",
"complexTypeName":"Carrier#Test",
"isScalar":false
}]
The Carrier entity is defined as below:
{
"shortName": "Carrier",
"namespace": "Test",
"isComplexType": true,
"dataProperties": [
{
"name": "Testing",
"isScalar":true,
"dataType": "String"
}
]
}
We are trying to return an array of complextype in breeze from a REST service call. We get an error in breeze.debug.js in the method proto._updateTargetFromRaw. The error is because the datatype is null.
Any idea how to fix this issue?
I'm guessing the problem is in your "complexTypeName". You wrote "Carrier#Test" when I think you meant to write "Carrier:#Test". The ":#" combination separates the "short name" from the namespace; you omitted the colon.
Hope that's the explanation.