0

I have list of string that I want to convert into list of object.

List<Warehouse> getWarehouseSuggestions(String query) {
    final warehouseList = warehouses.entries.toList();
    final locIds = warehouseList.map((e) => e.value.name);
    List<String> a = List.of(locIds).where((warehouse) {
      final warehouseLower = warehouse.toLowerCase();
      final queryLower = query.toLowerCase();

      return warehouseLower.contains(queryLower);
    }).toList();

    List<Warehouse> b = List<Warehouse>.filled(a.length, a);
    return b;
  }
3
  • 1
    List<Warehouse> b = a.map((string) => Warehouse(somethingRelatedToString)).toList(); Commented Jul 3, 2021 at 9:58
  • Wow! this solved my problem. Thanks a lot. Commented Jul 3, 2021 at 11:43
  • Glad to know that, I've added a general answer to this for future visitors. Commented Jul 3, 2021 at 11:48

2 Answers 2

1

You should use map on your List<String>, something like this:

List<Foo> foos = listOfString.map((s) => Foo(s)).toList();
// or
List<Foo> foos = listOfString.map(Foo).toList();
Sign up to request clarification or add additional context in comments.

2 Comments

I think is FooObject instead of Foo?
@JohnJoe Thanks for pointing it out. It's fixed now!
0

conversion from JSON(Map<String, dynamic>) to Map<DateTime, List<Object>>

date_StatDataMap: (map['date_StatDataMap'] as Map<String, dynamic>).map((key, value) => MapEntry(DateTime.parse(key), value.map((StatsData) {return StatsDataModel.fromMap(StatsData);}).toList())).cast<DateTime, List>(),

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.