List of string is getting duplicated - string

I am generating text fields dynamically and I am trying to get the contents of the text fields.
//I declared a list of string and generated it using the length of my product.
late final List<String> recovered;
//the length of the products is 3
recovered = List.generate(products.length, (index) => ""));
TextField(
onSubmitted: (value) {
recovered.insert(index, value);
log("the value is $value");
setState(() {});
},
controller: myControllers[index],
decoration: const InputDecoration(
enabledBorder:OutlineInputBorder(
borderSide: BorderSide(
width: 1,
color: Colors.grey)),
),
),
I inserted 1,2,4 into the textFields generated by my listView Builder and got the following values
[1, 2, 4, , , ] instead of [1, 2, 4].

Calling insert adds them to the list. I believe you want to replace them. For that, instead of
recovered.insert(index, value);
you need to do
recovered[index] = value;

on your Submit you need to check if the value is empty or not then add it to your list:
onSubmitted: (value) {
if(value.isNotEmpty){
recovered.insert(index, value);
log("the value is $value");
setState(() {});
}
},

Related

getting name attribute of a group fails (on the second time) in konvajs

I have 3 groups each contain a rect and a text. When creating each group I set "name" attribute to "false". Then I add these groups to an array called "groups". The problem is, that I want to get the "name" attribute when I do dragstart on one of these groups. I get the "name" only when I drag these groups for the first time. Meaning I startdrag the first - it works, second - works, third - works, but dragging one of them twice fails with "groups[i].name is not a function". That I dont understand, since it worked the first time.
this is how I created groups:
var group1 = new Konva.Group({
x: 30,
y: 50,
draggable: true,
name: "false",
});
var text1 = new Konva.Text({
fontSize: 26,
fontFamily: 'Calibri',
text: 'Hello',
fill: 'black',
padding: 10,
});
var rect1 = new Konva.Rect({
width: text1.width(),
height: text1.height(),
fill: '#aaf',
});
then I added them to a group like this:
group1.add(rect1).add(text1);
I created an array and pushed these groups in:
var groups = [];
groups.push(group1, group2, group3);
I iterate groups and assign each 'dragstart' event in which I came across my problem when logging
for (let i = 0; i < groups.length; i++) {
groups[i].on('dragstart', () => {
console.log(groups[i].name());
})

using for loop with push() to create arrays within a new array

I'm working on a project where I need to declare customsItem formatted in a particular way.
The format given is:
var customsItem = {
"description":"T-Shirt",
"quantity":20,
"net_weight":"1",
"mass_unit":"lb",
"value_amount":"200",
"value_currency":"USD",
"origin_country":"US",
};
In my project however, I have multiple descriptions, so I need to make customsItem an array containing both.
I have array itemInCart =
[
{
itemDescription: 't-shirt',
qty: 1,
pre_orderQty: 1,
price: 30,
weight: 8
},
{
itemDescription: 'pants',
qty: 0,
pre_orderQty: 1,
price: 40,
weight: 5
}
]
I need to get these items in the correct format and within an array called customsItem. I thought to do this using a for loop with push(). Currently, I'm not getting anything when I try to console.log(customsItem), so I'm not sure if this is the best way to achieve the results that I am trying to get. I would really appreciate any help or advice on how to correctly get the results that I need. Thank you!
const customsItem = [];
for (var item of itemInCart) {
const items = {
"description":item.itemDescription,
"quantity":item.qty + item.pre_orderQty,
"net_weight":item.weight,
"mass_unit":"oz",
"value_amount":item.price,
"value_currency":"USD",
"origin_country":"US",
}
customItem.push(
items
)
}
You are not pushing into the correct array:
const customsItem = [];
for (var item of itemInCart) {
const items = {
"description":item.itemDescription,
"quantity":item.qty + item.pre_orderQty,
"net_weight":item.weight,
"mass_unit":"oz",
"value_amount":item.price,
"value_currency":"USD",
"origin_country":"US",
}
customItem.push( <---- needs to be customsItem.push
items
)
}

How can i manage images dynamically using pdfkit in nodejs

Q : i wanted to manage images dynamically using pdfkit in nodejs. and also i want to addPage if images not fitted in current page.
Code :
const doc = new PDFDocument({
size: 'letter'
});
function photographs(doc) {
arr = [1, 2, 3, 4, 5, 6, 7]
doc
.fillColor('#1c5695')
.fontSize(10)
.font('Helvetica-Bold')
.text('PHOTOGRAPHS', 10, doc.y, {
width: 300,
align: 'left'
})
arr.forEach((element, i) => {
doc.image('images/pdflogo.png', {
align: 'center',
fit: [100, 100]
})
});
}
so my question is how to manage x and y position of image , so it'll manage space dynamically.
e.g first 6 img set into 1st row then rest will set on 2nd row and so on..

How to update entities in a table from an array?

I'm trying to figure out how to update many elements at once. Suppose I have the following array:
[
{
id: 100,
order: 1,
},
{
id: 101,
order: 2,
},
{
id: 102,
order: 3,
},
]
I then transform this array, replacing the values of order. The resulting array becomes the following:
[
{
id: 102,
order: 1,
},
{
id: 101,
order: 2,
},
{
id: 100,
order: 3,
},
]
I use this on the frontend to render a list in the appropriate order, based on the value of order.
But how can I update these 3 entities in my database?
I can obviously make 3 UPDATE statements:
const promises = [];
newArray.forEach(({ id, order }) => {
promises.push(
// executeMutation is just a custom query builder
executeMutation({
query: `UPDATE my_table SET order = ${order} WHERE id = ${id}'`
})
)
})
await Promise.all(promises)
But is it possible to do this in one query?
You can do this using the UNNEST function. First you'll need to handle the query parameters properly. https://www.atdatabases.org/ does this for you, otherwise you need to separately pass a string with placeholders and then the values. If you use #databases, the code could look like:
await database.query(sql`
UPDATE my_table
SET order = updates_table.order
FROM (
SELECT
UNNEST(${newArray.map(v => v.id)}::INT[]) as id,
UNNEST(${newArray.map(v => v.order)}::INT[]) as order
) AS updates_table
WHERE my_table.id = updates_table.id
`);
The trick here is that UNNEST lets you take an array for each column and turn that into a kind of temporary table. You can then use that table to filter & update the records.

how to fix the profanity_filter string cast error

I am trying to use ProfanityFilter in flutter, to filter bad words in a review content, which is a string. ProfanityFilter has a list of censored words, which I want to pass along with the list of swear words not included in a LDNOOBW list. However, since the content is a string, I use a cast, and then I get cast error: type 'ProfanityFilter' is not a subtype of type 'String' in type cast.
TextFormField(
decoration: InputDecoration(
prefixIcon: Icon(Icons.comment_outlined, color:
Colors.white60, size: 20,),
hintText: Languages
.of(context)
.revHint,
focusedBorder: UnderlineInputBorder(),
),
maxLines: null,
onChanged: (val) {
setState(() {
val = ProfanityFilter.filterAdditionally(badString) as String;
content = val;
viewModel.setDescription(content);
});
}
),
How do I pass string to a list, scan and then parse the censored list to a string back? Or is there a better and easier way to censore bad words? A map maybe? Thank you! Very new to flutter and coding.
Based on your response in comments, you are using the wrong method to do this. You should be using .hasProfanity.
Methods
hasProfanity - Detect if a string contains profanity
Use the hasProfanity() method of the filter instance. Pass in the string to be tested.
Example
final filter = ProfanityFilter();
String badString = 'you are an ass';
filter.hasProfanity(badString); //Returns true.
If you want to get a list of the bad words in the string then use the .getAllProfanity() method.
In your code, 'val' will contain the string entered by the user and this is what you pass to the method, not 'badString'.

Resources