How to upsert items in Flutter - list

I have a list
List<String> list = ['Hello']
I have TextField which has onChanged callback.
Question: How I can UPSERT an item to List when TextField triggers onChanged function
Wanted result:
['Hello', 'NewHello']
onChanged: (_) {
// What I should do with list. Thanks
}

You need to check if the list length is 2, if it is then update the value at index 1; if the length is less then simply add the change to the list.
Use the add method on the list to insert the change.
Change _ to text and then within the callback:
onChanged: (text) {
list.length == 2 ? list[1] = text : list.add(text);
}

Related

SwiftUI List: inserting item + changing section order = app crash

Please see the code below. Pressing the button once (or twice at most) is almost certain to crash the app. The app shows a list containing two sections, each of which have four items. When button is pressed, it inserts a new item into each section and also changes the section order.
I have just submitted FB9952691 to Apple. But I wonder if anyone on SO happens to know 1) Does UIKit has the same issue? I'm just curious (the last time I used UIkit was two years ago). 2) Is it possible to work around the issue in SwiftUI? Thanks.
import SwiftUI
let groupNames = (1...2).map { "\($0)" }
let groupNumber = groupNames.count
let itemValues = (1...4)
let itemNumber = itemValues.count
struct Item: Identifiable {
var value: Int
var id = UUID()
}
struct Group: Identifiable {
var name: String
var items: [Item]
var id = UUID()
// insert a random item to the group
mutating func insertItem() {
let index = (0...itemNumber).randomElement()!
items.insert(Item(value: 100), at: index)
}
}
struct Data {
var groups: [Group]
// initial data: 2 sections, each having 4 items.
init() {
groups = groupNames.map { name in
let items = itemValues.map{ Item(value: $0) }
return Group(name: name, items: items)
}
}
// multiple changes: 1) reverse group order 2) insert a random item to each group
mutating func change() {
groups.reverse()
for index in groups.indices {
groups[index].insertItem()
}
}
}
struct ContentView: View {
#State var data = Data()
var body: some View {
VStack {
List {
ForEach(data.groups) { group in
Section {
ForEach(group.items) { item in
Text("\(group.name): \(item.value)")
}
}
header: {
Text("Section \(group.name)")
}
}
}
Button("Press to crash the app!") {
withAnimation {
data.change()
}
}
.padding()
}
}
}
More information:
The error message:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView internal inconsistency: encountered out of bounds global row index while preparing batch updates (oldRow=8, oldGlobalRowCount=8)'
The issue isn't caused by animation. Removing withAnimation still has the same issue. I believe the issue is caused by the section order change (though it works fine occasionally).
Update: Thank #Yrb for pointing out an out-of-index bug in insertItem(). That function is a setup utility in the example code and is irrelevant to the issue with change(). So please ignore it.
The problem is here:
// multiple changes: 1) reverse group order 2) insert a random item to each group
mutating func change() {
groups.reverse()
for index in groups.indices {
groups[index].insertItem()
}
}
You are attempting to do too much to the array at once, so in the middle of reversing the order, the array counts are suddenly off, and the List (and it's underlying UITableView) can't handle it. So, you can either reverse the rows, or add an item to the rows, but not both at the same time.
As a bonus, this will be your next crash:
// insert a random item to the group
mutating func insertItem() {
let index = (0...itemNumber).randomElement()!
items.insert(Item(value: 100), at: index)
}
though it is not causing the above as I fixed this first. You have set a fixed Int for itemNumber which is the count of the items in the first place. Arrays are 0 indexed, which means the initial array indices will be (0...3). This line let index = (0...itemNumber).randomElement()! gives you an index that is in the range of (0...4), so you have a 20% chance of crashing your app each time this runs. In this sort of situation, always use an index of (0..<Array.count) and make sure the array is not empty.
I got Apple's reply regarding FB9952691. The issue has been fixed in iOS16 (I verified it).

Adding two types of object to mutableListOf<MyInterface>()

interface ListItem {
val style: ItemStyle
val identifier: ListItemIdentifier?
}
val mutableList = mutableListOf<ListItem>()
I have a list that I map to objects and group:
dataList.groupBy { it.type }.forEach { (type, itemList) ->
val type = TypeHeader(name = type.name )
val items = itemList.map { item ->
Item(
title = item.title,
subtitle = item.subtitle
)
}
mutableList.addAll(listOf(type , items ))
}
I need to add that objects to my mutableList but when I try
mutableList.addAll(listOf(type , items ))
there is a error
Type mismatch.
Required:
Collection<ListItem>
Found:
List<Any>
when I try cast listOf as ListItem app crashes
After some discussion in comments we got to the solution.
The problem is in this listOf() line. You try to mix type, which is a single item and items which is a list of items. listOf() does not magically flatten this to e.g.: [header, item, item, item]. It will create something like this instead: [header, [item, item, item]]. This is inferred to the list of Any objects, because some items are single objects and some are lists.
You can flatten header and items to a single list with:
listOf(header) + items
But in this case it is better to just add to mutableList twice:
mutableList.add(type)
mutableList.addAll(items)

APEX row selector

I'm displaying my results on an interactive grid. I'd like to be able to select multiple rows and click an edit button that will open up an “edit” form. I am having a number of problems:
Retrieve the car IDs of the rows selected. (I am having trouble accessing column values, I can access item values)
Pass a collection or array of ids to the edit form.
Save the collection.
Added more code in answer box by accident...……..
I made some progress but I am a little stuck. I followed the oracle blog and it was vey helpful. So on the attribute of the region I added the following code:
function (config) {
var $ = apex.jQuery,
toolbarData = $.apex.interactiveGrid.copyDefaultToolbar(),
toolbarGroup = toolbarData.toolbarFind("actions3");
toolbarGroup.controls.push(
{
type: "BUTTON",
action: "updateCar",
label: "Edit Selected Cars",
hot: true,
});
config.toolbarData = toolbarData;
config.initActions = function (actions)
{
// Defining the action for activate button
actions.add(
{
name: "updateCar",
label: "Edit Selected Cars",
action: updateCar
});
}
function updateCar(event, focusElement)
{
var i, records, model, record,
view = apex.region("ig_car").widget().interactiveGrid("getCurrentView");
var vid = "";
model = view.model;
records = view.getSelectedRecords();
if (records.length > 0)
{
for (i = 0; i < records.length; i++)
{
record = records[i];
//alert("Under Development " + record[1]);
vid = vid + record[1] + "||";
apex.item("P18_CAR").setValue(vid);// This is not needed just to test
//the output
// call next page
// pass array as sql source or directly on page
}
}
}
return config;
}
This works. A button is displayed and when selected it gets the values from the interactive grid. The part I am stuck is how to call the next page and pass the multiple values (2 columns) to the page to be displayed and in a query to do an update.
Thank you if you can help me accomplish this!

How to get a reference to the list of a Flex List in Mouse Down event

I have a list in mxml. I need to show a menu when the user longpresses an item in the list. The menu will show some action on the item that has been pressed on.
I also have to make the pressed item the selected item in the list. So I need a reference to the list. I cannot find a normal way to get to the list so I did this:
var list:Object = event.currentTarget.parent.parent.parent.parent.parent
Which of course is hideous. I am looking for a better way to get a reference to the list.
Here is my code for the list:
<s:List id="catList" x="0" y="0" width="100%" height="100%" click="selectItemHandler(event)">
<s:itemRenderer>
<fx:Component>
<s:IconItemRenderer
styleName="labelFontStyle"
messageStyleName="descriptionFontStyle"
labelField="labelField"
messageField="descriptionField"
dataChange="onDataChange(event)"
mouseDown="onMouseDown(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function onMouseDown(event:MouseEvent):void
{
try
{
var tg:Object = event.target;
var selectedItem:Object = event.currentTarget.data;
if (selectedItem != null)
{
// Here I need to set the selectedItem property of
// the owning list.
// I don't know how to get to the list so I did this.
var list:Object = event.currentTarget.parent.parent.parent.parent.parent;
list.selectedItem = selectedItem;
}
} catch (e:Error) {}
}
]]>
</fx:Script>
</s:IconItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
I'm not sure but won't just set the selected property in the itemrenderer's onMouseDown do the trick ?
selected = true;
If not, check if this will get you your list:
var myList:List = owner as List;
Another approach would be to create a custom event that contains your item and fire it from the itemrenderer. Then listen for that event on the list and set the selectedItem property to the item you got in the event

Sitecore. Read items from placeholder

I have the page with placeholder and list. I want to be able to fill the list with information depending on the items in the placeholder. For example:
I have "Person" template with Name and Image Fields. I creat few items based on that template (person A, B and C). After adding the renderings with these items as a source, I display images in the placeholder. Now I want to get all the item that are currently in the placeholder and write theirs Name field into the list(which is outside placeholder).
For now I was just able to get the rendering item, but not source item, out of placeholder.
You can try bellow method:
/// <summary>
/// Return all renderings to be rendered in a specific placeholder on the "default" device
/// </summary>
private IEnumerable<Sitecore.Data.Items.RenderingItem> GetRenderings(string placeholderKey, Sitecore.Data.Items.Item item)
{
Sitecore.Layouts.RenderingReference[] renderings = GetRenderingReferences(item, "default");
foreach (var rendering in renderings)
{
if (rendering.Placeholder == placeholderKey)
{
yield return rendering.RenderingItem;
}
}
}
I was able to get source items by Id that I found in Settings of RenderingItem:
var ph = "my_placeholder";
var renderingReferences = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true);
var renderingsInPlaceholder = renderingReferences.Where(r => r.Placeholder.EndsWith('/' + ph, StringComparison.OrdinalIgnoreCase));
var items = renderingsInPlaceholder.Select(x => context.GetItem(ID.Parse(x.Settings.DataSource)));