Skip to content

Instantly share code, notes, and snippets.

@kobeumut
Last active October 7, 2021 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobeumut/22c27a25a78895cf507f99085d2a2bb3 to your computer and use it in GitHub Desktop.
Save kobeumut/22c27a25a78895cf507f99085d2a2bb3 to your computer and use it in GitHub Desktop.
This extension is used when the list is unknown. So if the list is empty or the calling position is not available, shows an error which is Index out of range or null point exception. This extension provides easy usage to get data.
extension CheckData on List? {
T getItem<T>(int position,{dynamic errorReturnValue = "."}) => (this?.length ?? 0) > position ? this![position] : errorReturnValue;
}
//USAGE
// var list = [1,2,5,7];
// print("${list[4]}"); this shows index out of range error so now you can use like below
// list.getItem(4) it's return dot because of the default errorReturnValue is .
//
// if you want specify the error value you can enter errorReturnValue parameter
//
// list.getItem(4, errorReturnValue: "there is no value");
//
void main() {
List? list = [1,3];
print("${list.getItem(2, errorReturnValue: "there is no value")}");
}
@kobeumut
Copy link
Author

kobeumut commented Oct 7, 2021

You can try on dartpad.

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