What is the true meaning of pass-by-reference in modern languages like Dart?

Quick answer: what gets passed to your functions cookEggs and cookOne are references to the objects, not to the variables (which would be real pass-by-reference). The term pass-by-reference is often misused to mean pass-references-by-value: many languages only have pass-by-value semantics, where the values that are passed around are references (i.e. pointers, without the dangerous features). … Read more

How can I compare Lists for equality in Dart?

To complete Gunter’s answer: the recommended way to compare lists for equality (rather than identity) is by using the Equality classes from the following package import ‘package:collection/collection.dart’; Edit: prior to 1.13, it was import ‘package:collection/equality.dart’; E.g.: Function eq = const ListEquality().equals; print(eq([1,’two’,3], [1,’two’,3])); // => true The above prints true because the corresponding list elements … Read more

how to solve flutter CERTIFICATE_VERIFY_FAILED error while performing a POST request?

Just for the sake of clarity specially for the newcomers to Flutter/Dart, here is what you need to do in order to enable this option globally in your project: In your main.dart file, add or import the following class: class MyHttpOverrides extends HttpOverrides{ @override HttpClient createHttpClient(SecurityContext? context){ return super.createHttpClient(context) ..badCertificateCallback = (X509Certificate cert, String host, … Read more

Flutter: Run method on Widget build complete

You could use https://github.com/slightfoot/flutter_after_layout which executes a function only one time after the layout is completed. Or just look at its implementation and add it to your code 🙂 Which is basically void initState() { super.initState(); WidgetsBinding.instance .addPostFrameCallback((_) => yourFunction(context)); }

The default ‘List’ constructor isn’t available when null safety is enabled. Try using a list literal, ‘List.filled’ or ‘List.generate’

Short answer: Instead of the pre-null-safety operations var foo = List<int>(); // Now error var bar = List<int>(n); // Now error var baz = List<int>(0); // Now error use the following: var foo = <int>[]; // Always the recommended way. var bar = List.filled(1, 0); // Not filled with `null`s. var baz = List<int>.empty(); Long … Read more

What is the difference between the “const” and “final” keywords in Dart?

There is a post on dart’s website and it explains it pretty well. Final: “final” means single-assignment: a final variable or field must have an initializer. Once assigned a value, a final variable’s value cannot be changed. final modifies variables. Const: “const” has a meaning that’s a bit more complex and subtle in Dart. const … Read more

How do you build a Singleton in Dart?

Thanks to Dart’s factory constructors, it’s easy to build a singleton: class Singleton { static final Singleton _singleton = Singleton._internal(); factory Singleton() { return _singleton; } Singleton._internal(); } You can construct it like this main() { var s1 = Singleton(); var s2 = Singleton(); print(identical(s1, s2)); // true print(s1 == s2); // true }

How do I format a date with Dart?

You can use the intl package (installer) to format dates. For en_US formats, it’s quite simple: import ‘package:intl/intl.dart’; main() { final DateTime now = DateTime.now(); final DateFormat formatter = DateFormat(‘yyyy-MM-dd’); final String formatted = formatter.format(now); print(formatted); // something like 2013-04-20 } There are many options for formatting. From the docs: ICU Name Skeleton ——– ——– … Read more