Last active
October 7, 2021 12:30
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can try on dartpad.