Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. /**
  2. * Copyright (c) 2019 formation-dart-flutter. All rights reserved
  3. *
  4. * REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS,
  5. * WITH OR WITHOUT MODIFICATION, ARE NOT PERMITTED.
  6. *
  7. * DO NOT ALTER OR REMOVE THIS HEADER.
  8. *
  9. * Author : rushio consulting
  10. */
  11.  
  12. // main() - The special entry point, required, top-level function where app execution starts.
  13. // This is where the app starts executing.
  14. // Returns void and has an optional List<String> parameter for arguments.
  15. main(List<String> args) {
  16. print('Result with ??');
  17. print(nullaware_1(null));
  18. print(nullaware_1(456));
  19. print('----------------');
  20.  
  21. //
  22. print('Result with ??=');
  23. print(nullaware_2(1234, 0909));
  24. print(nullaware_2(null, 0909));
  25. print('----------------');
  26.  
  27. //
  28. print('Result with ?.');
  29. print(nullaware_3("je dois être en uppercase"));
  30. print(nullaware_3(null));
  31. print('----------------');
  32.  
  33. //
  34. print('Result with ...?');
  35. print(nullaware_4([1, 2, 3, 4], null));
  36. print(nullaware_4([1, 2, 3, 4], [5, 6, 7]));
  37. print('----------------');
  38. }
  39.  
  40. // ??
  41. // Use ?? when you want to evaluate and return an expression
  42. // IFF another expression resolves to null
  43. // ((x) => x == null ? otherExp : x)(exp)
  44. //
  45. // Example
  46. int nullaware_1(int input) => input ?? 12345;
  47.  
  48. // ??=
  49. // Use ??= when you want to assign a value to an object IFF
  50. // that object is null. Otherwise, return the object.
  51. // Assign the value only if the variable is null
  52. //
  53. // Example
  54. int nullaware_2(int firstValue, int secondValue) {
  55. int result = firstValue;
  56. result ??= secondValue;
  57. return result;
  58. }
  59.  
  60. // ?.
  61. //Use ?. when you want to call a method/getter on an object IFF
  62. // that object is not null (otherwise, return null).
  63. // ((x) => x == null ? null : x.method())(obj)
  64. // You can chain it
  65. // obj?.child?.child?.getter
  66. //
  67. // Example
  68. String nullaware_3(String firstValue) => firstValue?.toUpperCase();
  69.  
  70. // ...? Spread Operator with null aware [dart 2.3]
  71. //
  72. //
  73. /*
  74. List numbers = [];
  75. numbers.addAll(lowerNumbers);
  76. if(upperNumbers != null){
  77. numbers.addAll(upperNumbers);
  78. }
  79. */
  80. List<dynamic> nullaware_4(List<dynamic> lowerList, List<dynamic> upperList) =>
  81. [...lowerList, ...?upperList];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement