Related
I have a Lottie json file which i am using in the project. I am able to render the Lottie animation successfully. My question is i want to update the asset from the json file in order to update the icon on Lottie animation.
I have tried below but it doesnt work. It shows Lottie animation with the default value and after that its shows blank.
Lottie file
{
"v": "4.8.0",
"meta": {
"g": "LottieFiles AE 3.1.1",
"a": "",
"k": "",
"d": "",
"tc": ""
},
"fr": 30,
"ip": 0,
"op": 300,
"w": 400,
"h": 400,
"nm": "Comp 2",
"ddd": 1,
"assets": [
{
"id": "image_0",
"w": 32,
"h": 32,
"u": "",
"p": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAAABAAAAAQBPJcTWAAAAJHpUWHRDcmVhdG9yAAAImXNMyU9KVXBMK0ktUnBNS0tNLikGAEF6Bs5qehXFAAADdUlEQVRYhc2XzWsbRxjGfzO7lTCx15ccShVBfUvIQerF1L44l8TQkw1tc6xS7JOJiZN723/AaXDoqaEVPRlhsE+FHgIWBpvqUuvg6NiCvKFQXSS1CK13d3rY9Uqrr518QZ7T7ny87/POO/O8MwJN/PtDJm/4agXELVBLo0eJMqhDT4qD6Q37VMeuSBrQ2ckUlFAPBOR0yQIoqAolnkxt2sXXItD5/sOPMY3i+Gh1Icq4XmFq6++/tAn89/SjFQlFYPbNnEdo+lC4cv/lwWCHHGzo7GQKEvbfonOAWQn7nZ1MYbAjtgJh5Pu6Vs3cOqpdx7ePUd2W1hwfVvtXIiIQ5Fyeohm5SFuk12sAdH/5FDO3BoBXK+E3ziZNbeL6+cs90UuBaRQnORdpC3P+ETKzAIDMLAKg2ueoVh3jxl2M3BqkrSTus6GvwC0EeU/a7TKziDn/EHgIThvVbQYEWvWAVGoGAN8+SSIAqKXOTqYwtWkXJYAS6oHGLFTjRfCRmkHMXAuJLZBa3Yv6hZXVMRX5FKHC/aE1C0it7kVpGAev+gy3sp24MT0pPpGBvOpDtc97P04bZ/cO7tF3sXYjt8YHn/2UaMvw1YoMtF0f/dH79jF+4wy3+iPO7u1eisJx8urNBGvilnwVqRVWNso9gH/e23Cq28K3j+MTEk+EWjJ1nQNDuZfXFjCcJqpVR1hZjBt34+Yn6wEQHkN9Aovx/7ll5NzyyLFu5bGWOg7VgskEeiugGi/wqs9ieb+E/+dvuJVtPZu6zgfz79VKXBx9S3f3Ns7+5/ENOLeMOf/o7RAQVjbI7/UvY+39G863T3B+/TpuOEErLmGG16ixJyG1uheLHACnPVRwhsZoQZRNUIfASALy6s3RhlMzpL/6HdU4w//nDJG2hk9Av2CNhTpMlGJhZQNRySxiXP9Cw2gAZ/dOUlkOpHh6wz5VUB3LsVXHq5Vwj76J2tzK4/EROm0unm8lOldQnd6wT00AocQThPp50oT++u9WtjHz6wBcPN9CterROL1yHPokFKKpTbvYeZpwJwhl1bdPAo0P679XK2k5HHBfvryu95TQ9QqTrmRerYRXK0X1XjfSEWjieoWISn/Pq15KXweDl9KYEF25//IAJe69M+9K3Bt8G7x/DxMIV8L184FKvilEGdfPj3IO7/PjdBDv6nn+P/q8gqi9Sz0EAAAAAElFTkSuQmCC",
"e": 1
}
],
"layers": [
{
"ddd": 1,
"ind": 1,
"ty": 2,
"nm": "Layer 1",
"refId": "image_0",
"sr": 1,
"ks": {
"o": {
"a": 0,
"k": 100,
"ix": 11
},
"rx": {
"a": 0,
"k": 0,
"ix": 8
},
"ry": {
"a": 0,
"k": 0,
"ix": 9
},
"rz": {
"a": 0,
"k": 0,
"ix": 10
},
"or": {
"a": 0,
"k": [
0,
0,
0
],
"ix": 7
},
"p": {
"a": 1,
"k": [
{
"i": {
"x": 0.833,
"y": 0.833
},
"o": {
"x": 0.167,
"y": 0.167
},
"t": 0,
"s": [
65,
217,
0
],
"to": [
41.667,
0,
0
],
"ti": [
-41.667,
0,
0
]
},
{
"t": 96,
"s": [
315,
217,
0
]
}
],
"ix": 2
},
"a": {
"a": 0,
"k": [
16,
16,
0
],
"ix": 1
},
"s": {
"a": 0,
"k": [
100,
100,
100
],
"ix": 6
}
},
"ao": 0,
"ip": 0,
"op": 300,
"st": 0,
"bm": 0
}
],
"markers": []
}
This is how i am trying to change the asset from code.
class LottieSample extends StatelessWidget {
LottieSample({Key? key}) : super(key: key);
LottieImageAsset updatedAsset = LottieImageAsset(
width: 32,
height: 32,
id: 'image_0',
fileName:
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANsAAADmCAMAAABruQABAAAAhFBMVEUAAAD39/f////7+/vb29v19fX29vbk5OTIyMjg',
dirName: '');
#override
Widget build(BuildContext context) {
var asset2 = Lottie.asset(
'assets/lottie/revolve.json',
delegates: LottieDelegates(
image: (LottieComposition composition, LottieImageAsset asset) {
if (asset.id == 'image_0') {
asset = updatedAsset;
}
if (composition.images.containsKey('image_0')) {
print(composition.images['image_0']?.dirName);
print(composition.images['image_0']?.loadedImage);
print(composition.images['image_0']?.fileName);
composition.images.update(
'image_0',
(value) => updatedAsset);
}
},
),
);
return Container(
color: Colors.white,
child: asset2,
);
}
}
referring to the comments:
Write to a file
Future<String> getFilePath() async {
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); // 1
String appDocumentsPath = appDocumentsDirectory.path; // 2
String filePath = '$appDocumentsPath/demoTextFile.txt'; // 3
return filePath;
}
void saveFile() async {
File file = File(await getFilePath()); // 1
file.writeAsString("This is my demo text that will be saved to : demoTextFile.txt"); // 2
}
So you fetch the Lottie file once on the initial start-up of the app on that device and store it locally. Now you can update it any time you like by replacing the file.
{
"customer": "62f75f6204a24bb48edae723",
"product": "62cd46a3b325452b3efc6dd3",
"downPayment": 140,
"planOfInstallment": 12,
"moneyRequiredToPay": 629,
"contractInitiated": false,
"contractStatus": "Normal",
"moneyRecieved": 0,
"investor": [
{
"investorDetail": "62f7542289326e783ae7feba",
"money": 200,
"moneyRecived": 10,
"totalEarning": 250,
"monthlyEarning": 10,
"_id": "630e87abf5c87d202c27a2f8"
},
{
"investorDetail": "62f7542289326e783ae7feba",
"money": 170,
"moneyRecived": 8,
"totalEarning": 210,
"monthlyEarning": 8,
"_id": "630e87abf5c87d202c27a2f9"
}
],
"createdDate": "2022-08-30T21:55:18.917Z",
"_id": "630e87abf5c87d202c27a2f7",
"paymentschedule": [
{
"monthName": "September",
"dateToPay": "2022-09-25T21:00:00.000Z",
"paid": false,
"payment": 52.416666666666664,
"paymentRecieveDate": null,
"_id": "630e87abf5c87d202c27a2fa"
},
{
"monthName": "October",
"dateToPay": "2022-10-25T21:00:00.000Z",
"paid": false,
"payment": 52.416666666666664,
"paymentRecieveDate": null,
"_id": "630e87abf5c87d202c27a2fb"
},
{
"monthName": "November",
"dateToPay": "2022-11-25T21:00:00.000Z",
"paid": false,
"payment": 52.416666666666664,
"paymentRecieveDate": null,
"_id": "630e87abf5c87d202c27a2fc"
}
],
"documentContract": [],
"__v": 0,
"id": "630e87abf5c87d202c27a2f7"
}
This is my structure for example the person pay his monthly installment of _id: 630e87abf5c87d202c27a2fa and payment paid status is true so then i need that in investor array very object has monthlyEarning is added to moneyRecieved
I need api for that?
I am trying to insert data in the last element of an array in MongoDB. insert in the first element works using the 0 operator, but I'm confused about how to insert in the last element. To explain it, consider the following data:
[
{
"_id": "1",
"company": "turivius",
"forms": [
{
"current_version": 1,
"history": [
{
"content": [],
"layout": [],
"responses": [
{
"client_id": 100,
"response_date": datetime(2022, 5, 17, 3, 0),
"values": [
{
"field_id": 0,
"primeiro user": "primeiro form, resposta 1",
},
{
"field_id": 1,
"value": "aaa",
},
],
},
{
"client_id": 100,
"response_date": datetime(2022, 5, 17, 3, 0),
"values": [
{
"field_id": 0,
"primeiro user": "primeiro form, resposta 2",
},
{
"field_id": 1,
"value": "bbb",
},
],
},
],
"version": 0,
},
{
"content": [],
"layout": [],
"responses": [
{
"client_id": 100,
"response_date": datetime(2022, 5, 17, 3, 0),
"values": [
{
"field_id": 0,
"primeiro user": "primeiro form, resposta 1 v1",
},
{
"field_id": 1,
"value": "aaa",
},
],
}
],
"version": 1,
},
],
"id": "33b66684-24a9-4a45-a12f-27a330152ac8",
"is_active": True,
},
{
"current_version": 0,
"history": [],
"id": "fa2eb9a1-c7c4-4b64-b682-7f51658bc4ab",
"is_active": False,
},
],
"user_id": "1",
},
{
"_id": "2",
"company": "turivius",
"forms": [
{
"current_version": 0,
"history": [],
"id": "565656",
"is_active": True,
},
],
"user_id": "9999",
},
]
I want to insert data in the document with user_id = 1, form.id = 33b66684-24a9-4a45-a12f-27a330152ac8 and in the history array where the version = 1 I want to insert a new response:
{
"client_id": 100,
"response_date": datetime(2022, 5, 17, 3, 0),
"values": [
{
"field_id": 0,
"test": "test",
},
{
"field_id": 1,
"test2": "test3",
},
],
}
The history with version 1 is the last element, if I wanted insert in the first element the following code would works:
find_one_and_update(
{"$and": [{"user_id": "1"}, {"forms.id": '33b66684-24a9-4a45-a12f-27a330152ac8'}]},
{
"$push": {"forms.$.history.0.responses": data_here},
},
return_document=ReturnDocument.AFTER,
)
Is there anyway to do it that way or I need a more complex query to do it?
You want to add a new document to the responses array when those conditions are true:
{
user_id: '1',
'forms.id': '33b66684-24a9-4a45-a12f-27a330152ac8',
'forms.history.version': 1
}
forms is an array, so you want to update the array element that matches the condition, so you can use the $[identifier] operator. Inside the array of forms, you only want to update the history with a specific version, so you need to use the $[identifier] operator multiple times, the field you want to update would be like this:
forms.$[f].history.$[h].responses
f is the identifier of each elements in forms that matches the condition and h the elements of the history array that match the other condition. An arrayFilters object is used to define the conditions.
The complete query could be like this:
db.collection.update({
user_id: "1",
'forms.id': '33b66684-24a9-4a45-a12f-27a330152ac8',
'forms.history.version': 1
},
{
$push: {
'forms.$[f].history.$[h].responses': {
"client_id": 100,
"response_date": "datetime(2022, 5, 17, 3, 0)",
"values": [
{
"field_id": 0,
"test": "test",
},
{
"field_id": 1,
"test2": "test3",
},
],
}
}
},
{
arrayFilters: [
{
'h.version': 1
},
{
'f.id': '33b66684-24a9-4a45-a12f-27a330152ac8'
}
]
})
Working example at MongoDB playground
My colleague is response for backend development, this is one of example of the API response.
I want to know that how can I separate it to the map?
And There have same name, I am very difficult to handle it. JSON to Dart generated code also will have error case by same name.
Is there have any other solution to handle this Json?
{
"formId": 1,
"formTypeId": 2,
"formTemplateId": 1,
"version": 1,
"history": "N",
"refNum": "AGQ/T01/01/A1/QA2/0001",
"dateOfAcceptance": "2020/12/16 14:54:01",
"dateOfReacceptance": "2020/12/16 14:54:01",
"blockId": 242,
"floorId": 1,
"flatId": 98,
"isPublic": "N",
"locationId": 1,
"fromFloorId": 1,
"toFloorId": 2,
"elevationId": 1,
"isNextVendor": "Y",
"isMultipleVendor": "N",
"formStatusId": 1,
"checkResults": 1,
"submissionDate": "2020/12/16 14:54:01",
"submittedBy": "ADMINISTRATOR",
"siteId": "YLTL",
"formDetails": [
{
"formDetailsId": 1,
"formId": 1,
"title": "A. 驗收項目",
"checkType": 1,
"required": "Y",
"parentId": 0,
"orderNum": 1,
"formDetailsOptions": [],
"fromDetails": [
{
"formDetailsId": 3,
"formId": 1,
"title": "Title group",
"checkType": 2,
"required": "Y",
"parentId": 1,
"orderNum": 1,
"formDetailsOptions": [],
"fromDetails": [
{
"formDetailsId": 4,
"formId": 1,
"title": "門編號:",
"checkType": 5,
"required": "Y",
"parentId": 3,
"orderNum": 1,
"answerVal": "A001",
"formDetailsOptions": []
},
{
"formDetailsId": 5,
"formId": 1,
"title": "位置:",
"checkType": 6,
"required": "Y",
"parentId": 3,
"orderNum": 2,
"formDetailsOptions": [
{
"formDetailsOptionsId": 1,
"formDetailsId": 5,
"orderNum": 1,
"optionDesc": "單位大門",
"defectOpts": "N",
"additionOpts": "N",
"selected": "N"
},
{
"formDetailsOptionsId": 2,
"formDetailsId": 5,
"orderNum": 2,
"optionDesc": "廚房門",
"defectOpts": "N",
"additionOpts": "N",
"selected": "N"
},
{
"formDetailsOptionsId": 3,
"formDetailsId": 5,
"orderNum": 3,
"optionDesc": "廁所客廳",
"defectOpts": "N",
"additionOpts": "N",
"selected": "N"
}
]
},
{
"formDetailsId": 6,
"formId": 1,
"title": "假框抆塞:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 3,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 7,
"formId": 1,
"title": "門框尺寸:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 4,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 8,
"formId": 1,
"title": "門框木料:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 5,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 9,
"formId": 1,
"title": "門框接口:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 6,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 10,
"formId": 1,
"title": "門框色澤:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 7,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 11,
"formId": 1,
"title": "磨耳及蟻油:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 8,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 12,
"formId": 1,
"title": "防火膠條:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 9,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 13,
"formId": 1,
"title": "平正及穩固:",
"checkType": 4,
"required": "Y",
"parentId": 3,
"orderNum": 10,
"answerVal": "Y",
"formDetailsOptions": []
},
{
"formDetailsId": 14,
"formId": 1,
"title": "備註:",
"checkType": 7,
"required": "N",
"parentId": 3,
"orderNum": 11,
"answerVal": "Test group remark",
"formDetailsOptions": []
}
]
}
]
},
{
"formDetailsId": 2,
"formId": 1,
"title": "B. 要改善位置及詳情備註:",
"checkType": 1,
"required": "Y",
"parentId": 0,
"orderNum": 2,
"answerVal": "Test B remark",
"formDetailsOptions": []
}
],
"formDefects": [
{
"formDefectsId": 1,
"formId": 1,
"defectRemark": "Test Remark",
"followActionId": 1,
"reporterType": 1,
"formDefectsImgs": [
{
"formDefectsImgId": 1,
"formDefectsId": 1,
"imgType": 1,
"filePath": "data/images/1/DEFECT-20201228150623123-2F3F4H.jpg",
"orderNum": 1
}
]
}
],
"formVendors": [
{
"formVendorId": 2,
"formId": 1,
"vendorId": "3954",
"orderNum": 2,
"isNextVendor": "N",
"formVendorImgs": [
{
"formVendorImgId": 1,
"formVendorId": 2,
"filePath": "data/images/1/QUERY-20201228151423123-2FF2V4.png",
"orderNum": 1
}
],
"vendor": {
"vendorId": "3954",
"vendorDesc": "博西華",
"vendorDescDisp": "BSH"
}
}
],
"formNextVendors": [
{
"formVendorId": 1,
"formId": 1,
"vendorId": "3985",
"orderNum": 1,
"isNextVendor": "Y",
"formVendorImgs": [
{
"formVendorImgId": 2,
"formVendorId": 1,
"filePath": "data/images/1/QUERY-20201228151423123-VD5F5H.png",
"orderNum": 2
}
],
"vendor": {
"vendorId": "3985",
"vendorDesc": "瑞基",
"vendorDescDisp": "Zuki"
}
}
]
}
With this kind of complex json i would suggest using dart packages for serialization. For example:
https://pub.dev/packages/json_serializable
or
https://pub.dev/packages/built_value
If you can share Dart models, then use json_serializable package. That would make things easier. But...
...if you have to live with the JSON response that your colleague developed and you cannot share your models that convert from/to Json, one alternative would be to use https://app.quicktype.io/
I entered your JSON response and out came generated Dart code that seems legit, at a first glance at least.
Example from that code generation:
factory ComplexJson.fromJson(Map<String, dynamic> json) => ComplexJson(
formId: json["formId"],
formTypeId: json["formTypeId"],
formTemplateId: json["formTemplateId"],
version: json["version"],
history: historyValues.map[json["history"]],
refNum: json["refNum"],
dateOfAcceptance: json["dateOfAcceptance"],
dateOfReacceptance: json["dateOfReacceptance"],
blockId: json["blockId"],
floorId: json["floorId"],
flatId: json["flatId"],
isPublic: historyValues.map[json["isPublic"]],
locationId: json["locationId"],
fromFloorId: json["fromFloorId"],
toFloorId: json["toFloorId"],
elevationId: json["elevationId"],
isNextVendor: historyValues.map[json["isNextVendor"]],
isMultipleVendor: historyValues.map[json["isMultipleVendor"]],
formStatusId: json["formStatusId"],
checkResults: json["checkResults"],
submissionDate: json["submissionDate"],
submittedBy: json["submittedBy"],
siteId: json["siteId"],
formDetails: List<MDetail>.from(json["formDetails"].map((x) => MDetail.fromJson(x))),
formDefects: List<FormDefect>.from(json["formDefects"].map((x) => FormDefect.fromJson(x))),
formVendors: List<FormVendor>.from(json["formVendors"].map((x) => FormVendor.fromJson(x))),
formNextVendors: List<FormVendor>.from(json["formNextVendors"].map((x) => FormVendor.fromJson(x))),
);
A PostgreSQL 10 table contains JSON data like (here an SQL Fiddle):
[
{
"col": 7,
"row": 12,
"value": 3,
"letter": "A"
},
{
"col": 8,
"row": 12,
"value": 10,
"letter": "B"
},
{
"col": 9,
"row": 12,
"value": 1,
"letter": "C"
},
{
"col": 10,
"row": 12,
"value": 2,
"letter": "D"
}
]
How to extract only the "letter" values and concatenate them to a string like
ABCD
I suppose at the end I should use the ARRAY_TO_STRING function, but which JSON function to use for extracting the "letter" values to an array?
UPDATE:
Got a tip at the very helpful PostgreSQL mailing list too:
SELECT string_agg(x->>'letter','') FROM json_array_elements(
'[{"col": 7, "row": 12, "value": 3, "letter": "A"}, {"col": 8, "row": 12, "value": 10, "letter": "B"}, {"col": 9, "row": 12, "value": 1, "letter": "C"}, {"col": 10, "row": 12, "value": 2, "letter": "D"}]'::json
) x;
Use jsonb_array_elements() and string_agg():
with my_table(json_data) as (
values(
'[
{
"col": 7,
"row": 12,
"value": 3,
"letter": "A"
},
{
"col": 8,
"row": 12,
"value": 10,
"letter": "B"
},
{
"col": 9,
"row": 12,
"value": 1,
"letter": "C"
},
{
"col": 10,
"row": 12,
"value": 2,
"letter": "D"
}
]'::jsonb)
)
select string_agg(value->>'letter', '')
from my_table
cross join lateral jsonb_array_elements(json_data)
string_agg
------------
ABCD
(1 row)