Skip to content

Feature: firstOrElse variant in List #37372

Open

Description

@Antoniossss

It would be convinient to have variation of first property as method that would allow something to get first element, or produce default value insteaf of throwing wrong state exception..

Right now I am doing something like this (to avoid extra var)

var val=myList.where((i)=>i.x>5).map((i)=>i.y).firstWhere((v)=>true), orElse:()=>someDefaultValue

while I would like to terminate like this:
var val=myList.where((i)=>i.x>5).map((i)=>i.y).firstOrElse(()=>defaultValue);

Activity

  1. lrhn commented on Jun 26, 2019

    @lrhn
    Member

    It is unlikely that we'll change the List or Iterable APIs at the current time, there are far too many implementations that would then be broken.
    This functionality seems like something that can easily be implemented using extension methods, though, and anyone can add that extension.

    For now, you can do:

    bool kTrue(_) => true;
    var val = myList.where((i) =>i.x > 5).map((i) => i.y).firstWhere(kTrue, orElse: () => defaultValue) ;
  2. added
    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
    type-enhancementA request for a change that isn't a bug
    on Jun 26, 2019
  3. Antoniossss commented on Jun 26, 2019

    @Antoniossss
    Author

    I have no problem in extending API by myown for own purpose. Actually that is more than fine for me. Can you double check your link for extension methods? It seems to be dead .

  4. lrhn commented on Jun 27, 2019

    @lrhn
    Member

    Link fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-coretype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions