how could I add the elements of a List <dynamic> to a List <Object> using flutter - list

dart code where i try to add element in messages to finalList (Don't work)
List<Message> finalList = List(); //list where data should go
if (tmp != null) {
List<dynamic> messages = tmp["messages"]; //list where data come from
for (var element in messages) {
try {
Message eltMsg = Message.fromMap(element);
finalList.add(eltMsg);
} catch (e) {
print(e);
}
}
finalList.sort((a, b) => b.createdAt.compareTo(a.createdAt));
}

I assume the fromMap method accepts a Map<String, dynamic>, so if you are sure that the content of tmp["messages"] actually is a map, you can do the following:
final messages = List<Map<String, dynamic>>.from(tmp["messages"] as List<dynamic>);
List<Message> finalList = messages.map((m) => Message.fromMap(m)).toList();
finalList.sort((a, b) => b.createdAt.compareTo(a.createdAt));
As long as your implementation of Message.fromMap is correct, this should get you a list of messages.

Related

Comparing length of list in flutter bloc

I want to implement flutter bloc in my app. I want to compare the length of the list and trigger blocListener accordingly. But when using listenWhen, the parameter which it provides(previousState and currentState) both show the current length of the list. I expected that these two params will be giving different states through which I could compare the length of the list on different states.
BlocListener<ListBloc, ListCubitState>(
listener: (context, state){
print("new order");
},
listenWhen: (previous, current){
if(current.items.length > previous.items.length){
return true;
}
print(**current.items.length.toString()**);
print(**previous.items.length.toString()**);
return false;
},
My cubit class:
class ListBloc extends Cubit<ListCubitState>{
ListBloc(): super(ListCubitState(items: ["items", "items"]));
addItem() => emit(ListCubitState(items: addItemToList(state.items)));
removeItem() => emit(ListCubitState(items: removeItemToList(state.items)));
List<String> addItemToList(List<String> item){
List<String> newList = item;
newList.add("adsfasf");
return newList;
}
List<String> removeItemToList(List<String> item){
List<String> newList = item;
newList.removeLast();
return newList;
}
}
Could anyone suggest what's wrong in the code (or) Is there any other way to implement it?

adding record from one List to another List

In List One, I am getting some items. Each time those items are changing. Sometimes, I can get more than one record in the List.
In a second List, I would like to store all the data of List One. So, I can then display all the items of List Two.
To make it more clear.
List One = "/temp/file1.jpeg"
List Two = "/temp/file1.jpeg"
List One = "/temp/file2.jpeg"
List Two = "/temp/file1.jpeg,/temp/file2.jpeg"
I have tried this
void _openDocumentFileExplorer({fileType: FileType.custom}) async {
setState(() => _loadingPath = true);
try{
_paths = (await FilePicker.platform.pickFiles(
type: fileType,
allowMultiple: true,//_multiPick,
allowedExtensions: ['pdf']))?.files;
} on PlatformException catch (e) {
print("Unsupported operation" + e.toString());
} catch (ex) {
print('$ex');
}
if (!mounted) return;
setState(() {
_loadingPath = false;
_fileName = _paths != null ?
_paths!.map((e) => e.name).toString() : '...';
});
}
ListView.separated(
itemCount:
_paths != null && _paths!.isNotEmpty
? _paths!.length
: 1,
itemBuilder:
(BuildContext context, int index) {
final bool isMultiPath =
_paths != null && _paths!.isNotEmpty;
final String name = _paths!
.map((e) => e.name)
.toList()[index];
//filesGB store the full path + the file name
final filesGB = _paths!
.map((e) => e.path)
.toList()[index]
.toString();
print (filesGB);
_paths?.addAll(allFiles!.map((e) ));
allFiles.addAll(filesGB.toList());
allFiles.addAll(filesGB);
// allFilesV2.addAll(filesGB);
but it does not work. I am getting this error message.
"The argument type 'String' can't be assigned to the parameter type 'Iterable'"
Please, do you have any suggestion?
I think you can use SPREAD OPERATOR (...) using a triple dot for merging one array into another.
For example:
List list1= ["/temp/file1.jpeg"];
List list2 = [];
after some time
list1 = ["/temp/file2.jpeg"];
so whenever your list one change do
list2 = [...list2,...list1];
print(list2);
output: ["/temp/file1.jpeg","/temp/file2.jpeg"]
I think it would help.

trying to remove an item from List

I have a code working good, but i am trying to do an enhancement
https://trycf.com/gist/5fdbccd52121856991e6fe3f82307d34/lucee5?theme=monokai
in the above, i am trying if the deleted item in list is IN, it should also delete the other item starting with I letter
The code is looping for the list elements and doing a match to detect and delete the element
Source
<cfscript>
i = 'AS,AK,SK,SB,IN,IP';
Y = 'IN';
local.X = [];
listEach(I, function(value, index) {
if (!listFindNoCase(Y, value)) {
arrayAppend(X, value);
}
});
dump(x);
</cfscript>
You can do that by checking before if the list contains your element using listFindNoCase, then using listFilter to filter the items you do not want in your new list, something like this:
<cfscript>
originalList = 'AS,AK,SK,SB,IN,IP';
needle = 'IN,AS';
newList = originalList;
listEach(needle, function(needle) {
if (listFindNoCase(newList, needle)) {
newList = listFilter(newList, function(value) {
return lcase(left(value, 1)) != lcase(left(needle, 1));
});
}
});
dump(newList);
</cfscript>

My async call is returning before list is populated in forEach loop

I have a routine which gets a list of filenames from the device, then reads the file(s) to build a list. However, the calling routine always returns with zero items. I print the filenames, so I know they exist, however, it appears that the async is returning before I read the files. I used similar code when making an HTTP call. But, something here is causing the routine to return the list even though it hasn't completed. Perhaps, it is possible that I am calling it at the wrong time? I am calling retrieveItems here:
#override
void initState() {
super.initState();
retrieveItems();
}
Eventually I will have a refresh button, but for now I'd simply like the list to populate with the data from the files...
--------------------
Callee
Future<List<String>> readHeaderData() async {
List<String> l = new List();
List<String> files = await readHeaders(); // Gets filenames
files.forEach((filename) async {
final file = await File(filename);
String contents = await file.readAsString();
User usr = User.fromJson(json.decode(contents));
String name = usr.NameLast + ", " + usr.NameFirst;
print(name);
l.add(name);
}
return l;
Caller
void retrieveItems() async {
LocalStorage storage = new LocalStorage();
await storage.readHeaderData().then((item) {
try {
if ((item != null ) &&(item.length >= 1)) {
setState(() {
users.clear();
_users.addAll(item);
});
} else {
setState(() {
_users.clear();
final snackbar = new SnackBar(
content: new Text('No users found.'),
);
scaffoldKey.currentState.showSnackBar(snackbar);
});
}
} on FileNotFoundException catch (e) {
print(e.toString()); //For debug only
setState(() {
_users.clear();
});
});
}
});
This code
Future<List<String>> readHeaderData() async {
List<String> l = new List();
List<String> files = await readHeaders(); // Gets filenames
files.forEach((filename) async {
final file = await File(filename);
String contents = await file.readAsString();
User user = User.fromJson(json.decode(contents));
String name = user.NameLast + ", " + user.NameFirst;
print(name);
l.add(name);
}
return l;
}
returns the list l and then processes the asyc forEach(...) callbacks
If you change it to
Future<List<String>> readHeaderData() async {
List<String> l = new List();
List<String> files = await readHeaders(); // Gets filenames
for(var filename in files) { /// <<<<==== changed line
final file = await File(filename);
String contents = await file.readAsString();
User user = User.fromJson(json.decode(contents));
String name = user.NameLast + ", " + user.NameFirst;
print(name);
l.add(name);
}
return l;
}
the function will not return before all filenames are processed.
files.forEach((filename) async {
means that you can use await inside the callback, but forEach doesn't care about what (filename) async {...} returns.
Also possible
await Future.forEach(yourList, (T elem) async { ...async staff });
To expand on Günter's comment regarding using list.map(f), here's an example of converting a forEach call so that it works correctly.
Broken example
Incorrectly assumes forEach will wait on futures:
Future<void> brokenExample(List<String> someInput) async {
List<String> results;
someInput.forEach((input) async {
String result = await doSomethingAsync(input);
results.add(result);
});
return results;
}
Corrected example
Waits on the async functions to complete, using Future.wait and .map():
Future<void> correctedExample(List<String> someInput) async {
List<String> results;
await Future.wait(someInput.map((input) async {
String result = await doSomethingAsync(input);
results.add(result);
}));
return results;
}
I encountered the similar issue. The problem is that dart will NOT wait for "forEach" contrary to public believe. There are two solutions:
1) Convert forEach to for loop as indicated by others. Another is use Future:
2) await Future.forEach(list, (item) async {
// your code
final result = await getMyResult();
});
Another option
Future.wait(someList.map((item) => something_returns_future(item)));

Adding elements to List in Flutter from For statement?

I'm receiving the following error while trying to add elements from my for loop to my List...
NoSuchMethodError: The method 'addAll' was called on null.
Receiver: null
Tried calling: addAll("LrWr826cd3Y")
Here is my code...
Future getData() async {
//Map videoId;
String url = 'https://Youtube API';
var httpClient = createHttpClient();
var response = await httpClient.read(url);
Map data = JSON.decode(response);
var videos = data['items']; //returns a List of Maps
List searchTitles;
List searchIds;
List searchImages;
for (var items in videos) {
//iterate over the list
Map myMap = items; //store each map
final video = (myMap['id'] as Map);
print(video['videoId']);
searchIds.addAll(video['videoId']);
final details = (myMap['snippet'] as Map);
final videoimage = (details['thumbnails'] as Map);
final medium = (videoimage['medium'] as Map);
}
setState(() { });
if (!mounted) return;
}
print(video['videoId']); successfully lists the 3 Youtube video ids as Strings. searchIds.addAll(video['videoId']); throws the error. I've tried both searchIds.add and searchIds.addAll. Where am I going wrong?
I would like to eventually push these lists to my List class here..
class CardInfo {
//Constructor
List id;
List title;
List video;
CardInfo.fromJson(List json) {
this.id;
this.title;
this.video;
}
}
You are not instantiating your searchIds object. add this
List searchIds = new ArrayList<>();
(Or)
List searchIds = new List();