How to handle key prop in a list? - list

Can you help me try to correct this code ?
I would like to make sure I have a list of customer orders. Each order must be able to be unrolled so that we can access the details of the number of items, their detail and the price of the order.
I wanted to try to do this with react-native-paper and the list component of this package.
However, I get an error on the key of each item. How can I solve it?
Thanks for your help.
> Warning: Each child in a list should have a unique "key" prop.
import * as React from 'react';
import { ScrollView } from 'react-native';
import { List } from 'react-native-paper';
const Completed = () => {
const [expanded, setExpanded] = React.useState(true);
const handlePress = () => setExpanded(!expanded);
const list = [
{
title: 'Commande 1',
date:'01/01/2020',
icon: 'av-timer'
},
{
title: 'Commande 2',
icon: 'flight-takeoff'
},
{
title: 'Commande 3',
date:'01/01/2020',
icon: 'av-timer'
},
{
title: 'Commande 4',
date:'01/01/2020',
icon: 'flight-takeoff'
},
{
title: 'Commande 5',
date:'01/01/2020',
icon: 'av-timer'
},
{
title: 'Commande 6',
date:'01/01/2020',
icon: 'flight-takeoff'
},
]
return (
<ScrollView>
{
list.map((item, i) => (
<List.Section title={item.date}>
<List.Accordion
title={item.title}
left={props => <List.Icon {...props} icon="folder" />}>
<List.Item title="Nombre d'articles : ..." />
<List.Item title="Prix total : ...€" />
</List.Accordion>
</List.Section>
))
}
</ScrollView>
);
}
export default Completed;

Add key prop like
list.map((item, i) => (
<List.Section title={item.date} key={i.toString()}> // Like here
....
</List.Section>
))

Make sure to provide a key to the first item in any loop.
In your case add the key to List.Section
<List.Section key={item.title} title={item.date}>
........
</List.Section>
Key can be any unique string in your data

Related

In Kendo, when you enter data from the Combobox, the letters touch when converting html to pdf

I'm listing a survey. First, all the students on the list are listed. When a student chooses from the combobox, the answers of that student are listed. When I list all of them, when I transfer to pdf, it transfers beautifully. But when the student chooses, the Turkish characters change when transferring the pdf.
#(Html.Kendo().ComboBox()
.Name("ogrenciyeGoreComboBox")
.Filter("contains")
.AutoBind(true)
.DataTextField("AdSoyad")
.DataValueField("Id")
.Events(e =>
{
e.Change("onChangeOgrSecimi");
})
.HtmlAttributes(new
{ required = "required",
id = "OgrenciyeGoreCombo",
style = "background:#f7dcd4;"
})
.SelectedIndex(0)
.DataSource(sourceag =>
{
sourceag.Read(read =>
{
read.Action("AnketOgrenciSec", "Anket", new
{
ViewBag.AnketId
});
});
})
)
$("#exportAnket").click(function () {
kendo.drawing.drawDOM($("#divAnketSonucc"), {
paperSize: "A4",
landscape: true,
margin: { left: "1cm", top: "1cm", right: "1cm", bottom: "1cm" },
template: $("#page-template").html()
})
.then(function (group) {
console.log(group);
return kendo.drawing.exportPDF(group);
})
.done(function (data) {
kendo.saveAs({
dataURI: data,
fileName: "AnketSonucu.pdf"
});
});
});

how transform text to List flutter

I use this code to have two custom textfields (TodTextfield) and a button (TodButton).
Padding(
padding: const EdgeInsets.only(top: 20),
child: TodTextField.formField(
key: _dayKeys[1],
controller: _facebookController,
labelText: localizations.hintFacebook,
textInputAction: TextInputAction.next,
),
),
Padding(
padding: const EdgeInsets.only(top: 20),
child: TodTextField.formField(
key: _dayKeys[2],
controller: _siteController,
labelText: localizations.hintSite,
textInputAction: TextInputAction.done,
),
),
TodButton.elevated(
label: localizations.btnConfirm,
replaceOnPressed: true,
onPressed: () async {
final patch = await Connection()
.accessoryData(
unitId: widget.connectedUnit.id.toString(),
contactEmail: _emailController.text,
contactPhone: _phone,
//socialAccount: SocialAccount?,/// I NEED HELP HERE
)
},
),
class SocialAccount {
SocialAccount({
required this.type,
required this.link,
});
factory SocialAccount.fromJson(Map<String, dynamic> json) {
try {
return SocialAccount(
type: AccountType.fromName(json.get('type') as String),
link: json.get('link') as String,
);
} catch (e, stack) {
logger.e('Failed to deserialize SocialAccount', e, stack);
rethrow;
}
}
Map<String, dynamic> toJson() => {
'type': type.toString(),
'link': link,
};
final AccountType type;
final String link;
}
now I need to take the data entered by the user in the two Textfields to make a Patch, this one:
"social_accounts": [{
"type": "facebook" | "instagram" | "website" | "twitter"
"link": "string"
}]
SocialAccount is the class I made myself as a Model to manage that data.
The problem is that I don't know how to take those two texts I have, to put them in the call which is a list ..

Insert Input Chips From List<String> into TextFormField Flutter on Button Click

I am having a List<String> of cities below
List<String> cities = ["Nairobi", "Tokyo", "Moscow", "Bogota",
"Helsinki", "Denver", "Stockholm", "Oslo"];
I am taking the cities list and just setting the text to TextFormField using TextEditingController upon a city being selected in showCityPickerDialog which updates the cities string list. Below is the code
TextFormField(
cursorHeight: 20,
enableInteractiveSelection: false,
readOnly: true,
focusNode: FocusNode(),
controller: TextEditingController(
text:
cities.isEmpty ? null : "$cities"),
onTap: () {
showCityPickerDialog();
},
decoration: InputDecoration(
isDense: true,
labelText: "Select Cities",
border: const OutlineInputBorder(),
suffixIcon: IconButton(
onPressed: () {
showCityPickerDialog();
},
icon: const Icon(Icons.location_city)),
),
validator: (phoneNo) {},
keyboardType: TextInputType.multiline,
maxLines: null,
)
What i would like to achieve is instead of just setting the text using TextEditingController(text: cities.isEmpty ? null : "$cities") i would like to use Chips like in below code where the number of chips inside the TextFormField will depend on the length of the list
Chip(
avatar: CircleAvatar(
backgroundColor: Colors.grey.shade800,
child: Text('City Initial'),
),
label: Text('City'),
)
Maybe you can add chips inside your TextFormField decoration as prefix Widget like this:
prefix: Row(
children: cities
.map(
(e) => Chip(
avatar: CircleAvatar(
backgroundColor: Colors.grey.shade800,
child: Text('City Initial'),
),
label: Text(e),
),
)
.toList(),
)

Search bar with ListView possible in Flutter?

I want to implement a searchbar in my flutter application. I have to go through a listview out of ListTiles. Here I want to check if the title of the listtile contains the letters in the search field. Is this possible with a List?
It does not have to be with the title. It could be something else with what I can identify the Tile. But please, not the index, the user would not know it.
Is a List the right widget or do I have to use something else to implement a search Engine in my Application
Rather than using a 3rd party package, you can use native showSearch() function :
showSearch(context: context, delegate: ListSearchDelegate());
And then a class extending SearchDelegate:
class ListSearchDelegate extends SearchDelegate{
ListSearchDelegate({Key key,}): super() ;
List<String> listItems = <String>['One', 'Two', 'Three', 'Four', 'Five'] ;
#override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.clear),
onPressed: () {
query = '';
},
),
];
}
#override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
close(context, null);
},
);
}
#override
Widget buildResults(BuildContext context) {
List<String> subList ;
subList = query != '' ? listItems.where((item) => item.contains(query)).toList() :
listItems ;
return ListView.builder(
itemCount: subList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(subList[index]),
);
}
);
}
#override
Widget buildSuggestions(BuildContext context) {
return Container();
}
}
Try https://pub.dev/packages/flutter_search_panel
List<SearchItem<int>> data = [
SearchItem(0, 'This'),
SearchItem(1, 'is'),
SearchItem(2, 'a'),
SearchItem(3, 'test'),
SearchItem(4, '.'),
];
FlutterSearchPanel<int>(
padding: EdgeInsets.all(10.0),
selected: 3,
title: 'Demo Search Page',
data: data,
icon: new Icon(Icons.check_circle, color: Colors.white),
color: Colors.blue,
textStyle: new TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20.0, decorationStyle: TextDecorationStyle.dotted),
onChanged: (int value) {
print(value);
},
),

Sencha touch - trying to delete row from list

I try to get an editable list with this code:
var isEditing = false;
new Ext.Application({
launch: function(){
new Ext.Panel({
//layout: 'card',
fullscreen: true,
items: new Ext.List({
id: 'myList',
store: new Ext.data.Store({
fields: ['myName'],
data: [{ myName: 1 }, { myName: 2 }, { myName: 3}]
}),
itemSelector: '.x-list-item',
multiSelect: true,
itemTpl: '<span class="name">{myName}</span>',
tpl: new Ext.XTemplate(
'<tpl for=".">' +
'<div class="x-list-item">' +
'<tpl if="this.isEditing()">' +
'<img src="images/delete.gif" ' +
'onclick="Ext.getCmp(\'myList\').myDeleteItem({[xindex-1]})" ' +
'style="vertical-align: middle; margin-right: 15px;"/>' +
'</tpl>' +
'{myName}</div>' +
'</tpl>',
{
compiled: true,
isEditing: function () {
console.log('isEditing (tpl):' + isEditing)
return isEditing;
}
}),
myDeleteItem: function (index) {
var store = this.getStore();
var record = store.getAt(index);
console.log('removing ' + record.data.myName);
store.remove(record);
},
listeners: {
itemtap: function () {
if (isEditing){
console.log('isEditing: ' + isEditing);
return;
}
},
beforeselect: function () {
console.log('isEditing: before ' + !isEditing);
return !isEditing;
}
}
}),
dockedItems: [{
dock: 'top',
xtype: 'toolbar',
layout: { pack: 'right' },
items: [
{
xtype: 'button',
text: 'Edit',
handler: function () {
var list = Ext.getCmp('myList');
if (!isEditing)
list.mySelectedRecords = list.getSelectedRecords();
isEditing = !isEditing;
this.setText(isEditing ? 'Save' : 'Edit');
list.refresh();
if (!isEditing)
list.getSelectionModel().select(list.mySelectedRecords);
}
}]
}]
});
}
});
but its not working like it should. If I press the EDIT button there is no delete-image and so there is no deleted item....
There are 3 things that I can see:
The Template is rendered once, you will need to call .refresh() or .refreshNode() on the list to update any item templates. The better way to accomplish this would be to hide the delete button via CSS and display it when the 'edit' button is clicked.
There is probably a naming conflict between the isEditing variable declared at the top and the isEditing function reference. It is very confusing to have these two things named the same, and can lead to problems with variable scoping.
The click event that you are looking for may be intercepted by the parent list item and Sencha Touch is turning it into a 'itemtap' event on the list item.
I was not able to delete until I added an id field without a datatype to my model. I don't know why as it should know which record to delete via the index.
Ext.regModel('Setting', {
fields: [
{name: 'id'}, // delete works after adding
{name: 'name', type: 'string'}
],
proxy: {
type: 'localstorage',
id: 'settings'
}
Kevin