Use parameter in list | Flutter/Dart [duplicate] - list

This question already has answers here:
Error: The instance member ... can't be accessed in an initializer
(4 answers)
Closed 1 year ago.
I´m new to flutter and got a question that makes me crazy for 2 days :)
I want to use a parameter(name1) in a list, but can´t figure out what went wrong:
List<Widget> player = [Text(name1)];
String name1 = 'Max';
#override
Widget build(BuildContext context) {
return Container(
child: player[0],
);
}
}
Error: Can't access 'this' in a field initializer to read 'name1'
This is a simplified version, but includes the problem.

You cannot use a just initialized variable to initialize another variable. You can call the variable inside a method. For example if is a StatefulWidget, you can fill the list inside the initState method.
List<Widget> player = [];
String name1 = 'Max';
#override
void initState() {
player.add(Text(name1));
super.initState();
}
Or you can initialize the list with all the widgets already:
List<Widget> player = [Text('name1'),Text('name2')];

Related

Get the size of a QStringList in QML from a property passed from C++ backend

Surely is a silly question but i can't get out of it...
Is there any method in the QML side to get the size of a QStringList passed as a property from the c++ backend?
I can use the property to fill the model of a combobox but i can't find a straight way to get the size (of course i can expose an invokable method from the cpp but it's not what i want)
example:
test.h
class Test : public Workflow
{
Q_OBJECT
Q_PROPERTY(QStringList availableCameras MEMBER m_availableCameras NOTIFY availableDevicesChanged)
[...]
private:
qStringList m_availableCameras
test.qml
GroupBox{
anchors.fill: parent
title: "Camera Panel"
property string selectedCamera: ""
function showImage(){
if(test.availableCameras.size() === 1) // NOT WORKING
{
return configurator.lastSingleImage
}
if(selectedCamera === test.rightCamSerialConf)
{
return configurator.lastRightImage
}
if(selectedCamera === test.leftCamSerialConf)
{
return configurator.lastLeftImage
}
}
ComboBox{
model: test.availableCameras // WORKING
editable: false
onEditTextChanged: selectedCamera = editText
}
[...]
}
A QStringList acts as a JS array of strings in QML.
In general you can use Array's functions on it : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
And when you can't, you can explicitely make it an actual array with Array.from
In your case you want length:
if(test.availableCameras.length === 1)

How to sort List of instance class depend on its property in dart or flutter [duplicate]

This question already has answers here:
Sort a list of objects in Flutter (Dart) by property value
(12 answers)
Closed 1 year ago.
i have a List<Contact> where the class Contact like this below:
class Contact {
Contact({
this.id,
this.name,
this.phone,
this.job,
this.company,
this.image,
this.email,
});
int? id;
String? name;
String? phone;
String? job;
String? company;
String? image;
String? email;
}
the question is, how to sort the List by comparing the Contact.name?
because i want sort the list ascending A-Z
You can pass a comparison function to List.sort.
someObjects.sort((a, b) => a.someProperty.compareTo(b.someProperty));
or List mList = (mList..sort()).reversed.toList();
you can be helped from this link Sort a list of objects in Flutter (Dart) by property value

How to use a list in a constructor in flutter?

I want to use a list in another class. So my idea was to simply use a constructor, but i got a few problems. First of all i want to use the list for a PaginatedDataTable. Therefore I created a new class for the datasource and i want to use my list of DataRows in this class. Here is my constructor to get access to the list:
class ExerciseDataSource extends DataTableSource {
ExerciseDataSource({Key key, this.list}) : super(key: key);
final List<DataRow> list;
I got an error for using the key, saying 'The named parameter 'key' isn't defined.'
I also got an error in my class, where i have my list of DataRows named _rowList with the error 'The instance member '_rowList' can't be accessed in an initializer.':
class _ExerciseTableState extends State<ExerciseTable> {
final ExerciseDataSource _rowsDataSource = ExerciseDataSource(list: _rowList);```
If you look at the definition of DataTableSource you will notice it extends aChangeNotifier, not a Widget.
You can't use a property of the class as a property of another property. You could modify your code to something like this:
class __ExerciseTableState extends State<_ExerciseTable> {
final List _rowList = [];
ExerciseDataSource _rowsDataSource;
#override
void initState() {
_rowsDataSource = ExerciseDataSource(list: _rowList);
super.initState();
}
#override
Widget build(BuildContext context) {
return Container();
}
}

Flutter: Passing list from one page to another page

I working on a flutter project. I want to pass list from one page to another page. but it not working out for me
List list;
articleCall save = await Navigator.of(context).push(new MaterialPageRoute<articleCall>(
builder: (BuildContext context) {
return new AddArticleDialog(list);
},
-----------
class AddArticleDialog extends StatefulWidget {
List ls=[];
AddArticleDialog({this.ls});
#override
AddArticleDialogState createState() => new AddArticleDialogState();
}
It says me the error: Too many positional arguments: 0 expected, but 1 found. Please help
Thanks,
Sathish
When braces {} are used on constructor parameters, it will become named parameters as mentioned in the docs.
Having the constructor as
List? ls=[]; // List should be nullable since the argument is optional
AddArticleDialog({this.ls});
will require it to use an identifier for the argument.
return AddArticleDialog(ls: list);

swift3 - Singleton [duplicate]

This question already has answers here:
Using a dispatch_once singleton model in Swift
(30 answers)
Closed 6 years ago.
How can I convert this to Swift 3:
struct Static {
static var instance : myForm?
static var token : dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instance = myForm()
}
return Static.instance!
Just this:
static let instance = MyForm()
and call it
let form = MyForm.instance
A note from the documentation:
Stored type properties are lazily initialized on their first access. They are guaranteed to be initialized only once, even when accessed by multiple threads simultaneously, and they do not need to be marked with the lazy modifier.
PS: Consider that struct and class names are supposed to start with a capital letter.