List<Object> to Stream<List<Object>> Flutter? - list

How to Convert List to Stream<List> in Dart , Flutter. I am trying to convert List of Objects to Stream List.

you can do it like this :
Stream<List<String>> _currentString = watchForSomeStream();
_currentString.listen((listOfStrings) {
for (String myString in listOfStrings) {
print(myString);
}
});

Related

How To Retrieve Group Of Elements From IEnumerable Without Iterating

I have the following:
IEnumerable<Personel> personel= page.Retrieve<Personel>....
Then I have List which contains only personelIDs
List<int> personelIDs....
I need to retrived all 'personels' from the IEnumerable and assign it into a new List which matches the personelIDs from 'personelIDs ' list.
I can do it my iterating and having verify the IDs and if they're equal assign it into another List,
but is there a short here where I can retrieve it without iterating or having multiple lines of code?
Basically Is there a way on how to shortened this
List<int> pIds = ....// contains only specific personellID's
IEnumerable personelIEn = // contains Personel data like personel IDs, name..etc
List<Personel> personel = personelIEn.ToList();
List<Personel> personelByTag = new List<Personel>();
foreach (Personel b in personel ) {
if (pIds.Contains(b.DocumentID)) {
personelByTag .Add(b);
}
}
return personelByTag ;
basically I'm trying to find ways how to shortened the above code
You can use a predicate:
public List<Personel> search(String documentId, List<Personel> list)
{
Predicate<Personel> predicate = (Personel personel) => (personel.Id== documentId);
return list.FindAll(predicate);
}
Could that help?

How to count a specific length of a list in flutter

I created a task list. I want to count the tasks length like this:
ongoing tasks = task list length - isDone tasks
I want to find the isDone tasks count in this case. As to the picture isDone tasks = 2.
These are the classes that I created.
class Task {
final String taskName;
bool isDone;
Task({required this.taskName, this.isDone = false});
void toggleDone() {
isDone = !isDone;
}
}
........
class Taskdata with ChangeNotifier {
final List<Task> _tasks = [];
UnmodifiableListView<Task> get tasks {
return UnmodifiableListView(_tasks);
}
int get tasksCount {
return _tasks.length;
}
}
You can use the where function to filter a list per a certain condition:
int get tasksCount {
List<Task> tasksLeft = _tasks.where((task) => !task.isDone).toList(); //where returns an iterable so we convert it back to a list
return tasksLeft.length;
}
if something goes wrong, you may need to convert List to Iterable
int get doneCount {
Iterable<Task> tasksDone = _tasks
.where((task) => task.isDone)
.toList(); //where returns an iterable so we convert it back to a list
return tasksDone.length;
}

Flutter convert List<List<dynamic>>

I have a Flutter app in which I make an http request, which brings me json data. Afer formatting it with a package I get a list of lists of dynamic type. Here´s what it looks like:
[[2016-04-01, 85.5254], [2016-05-01, 89.1118], [2016-06-01, 91.8528], [2016-07-01, 93.7328], [2016-08-01, 93.9221], [2016-09-01, 95.0014], [2016-10-01, 97.2428], [2016-11-01, 98.8166]]
So I created a class named IpcData, which recieves a String and a double.
class IpcData {
final String date;
final double value;
IpcData(this.date, this.value);
}
So we could guess that an IpcData instance would look like the following:
IpcData(2016-08-01, 93.9221)
I can´t figure out how, but I´d like to have a method that using the information from the List<List<dynamic>> to return a List<IpcData>, that would looke like:
[IpcData(2016-08-01, 93.9221), IpcData(2016-08-01, 93.9221), IpcData(2016-08-01, 93.9221),]
You can use .map function on the original list to construct the new one. Something like this.
class IpcData {
final String date;
final double value;
IpcData(this.date, this.value);
#override
String toString() {
return '$date -> $value';
}
}
void main() {
List<List<dynamic>> initList = [
['2016-04-01', 85.5254], ['2016-05-01', 89.1118], ['2016-06-01', 91.8528],
['2016-07-01', 93.7328], ['2016-08-01', 93.9221], ['2016-09-01', 95.0014],
['2016-10-01', 97.2428], ['2016-11-01', 98.8166], ['2016-12-01', 99.8166]
];
List<IpcData> ipcList = initList.map((e) => IpcData(e[0], e[1])).toList();
print(ipcList);
}

Create List<Dynamic> from List<Stuff>

I am trying to create a .csv file using a List<Stuff> type list. In order to use ListToCsvConverter(), I need a List<Dynamic>.
How do I convert a List<Stuff> to a List<Dynamic> so that I can convert it to .csv with ListToCsvConverter()?
"Stuff" model below:
class Stuff {
int id;
String stuffType;
String stuffSize;
String stuffTime;
Stuff({this.id, this.stuffType, this.stuffSize, this.stuffTime});
}
Your problem is that ListToCsvConverter() is not accepting a List<Stuff> but a List<List<dynamic>>.
The first level List is the list of rows for your csv, while the second level List is the list of cells in each row.
I added a getter csvRow in your Stuff class and map to the rows just before calling ListToCsvConverter():
Full source code:
import 'package:faker/faker.dart';
import 'package:csv/csv.dart';
void main() {
String csv = const ListToCsvConverter()
.convert(stuffList.map((stuff) => stuff.csvRow).toList());
print(csv);
}
class Stuff {
int id;
String stuffType;
String stuffSize;
String stuffTime;
Stuff({this.id, this.stuffType, this.stuffSize, this.stuffTime});
List<dynamic> get csvRow => [id, stuffType, stuffSize, stuffTime];
}
final faker = Faker();
final stuffList = List.generate(
100,
(index) => Stuff(
id: faker.randomGenerator.integer(999999, min: 100000),
stuffType: faker.person.name(),
stuffSize: '${faker.randomGenerator.integer(1000)}MB',
stuffTime: faker.date.toString(),
));
Posting a clear answer for people to see.
Initial List is "stuffList".
How do I convert a List<Stuff> to a List<Dynamic> so that I can convert it to .csv with ListToCsvConverter()?
//Create a new dynamic list
List<List<dynamic>> rows = List<List<dynamic>>();
//Convert stuffList to List<List<dynamic>>
for (int i = 0; i < listStuff.length; i++) {
List<dynamic> row = List();
row.add(listStuff[i].id);
row.add(listStuff[i].stuffType);
row.add(listStuff[i].stuffSize);
row.add(listStuff[i].stuffTime);
rows.add(row);
}
//convert the list to csv
final csv = const ListToCsvConverter().convert(rows);

Is there a default String conversion method in Chapel?

Is there a default method that gets called when I try to cast an object into a string? (E.g. toString in Java or __str__ in Python.) I want to be able to do the following with an array of Objects, but some of them might be nil:
for item in array {
writeln(item : string);
}
First of all, casting nil to string isn't necessarily a problem:
class C {
var x:int;
}
var array = [ new C(1), nil:C, new C(2) ];
for item in array {
writeln( item : string );
}
outputs
{x = 1}
nil
{x = 2}
Secondly, if you did want to customize the output of your class C, you'd write a writeThis method (or a readWriteThis method). See The readThis(), writeThis(), and readWriteThis() Methods. The writeThis method will be called both for cast to string and also for normal I/O. For example:
class C {
var x:int;
proc writeThis(writer) {
writer.writef("{%010i}", x);
}
}
var array = [ new C(1), nil:C, new C(2) ];
for item in array {
writeln( "writing item : string ", item : string );
writeln( "writing item ", item);
}
outputs
writing item : string {0000000001}
writing item {0000000001}
writing item : string nil
writing item nil
writing item : string {0000000002}
writing item {0000000002}
There is more I could say about why it works this way, what it might do in the future, and the limitations of the current strategy... but a mailing list would be a better place for such discussion if you'd like to have it.