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

Difference between assigning the values in parameter list and initializer list

Dart constructs objects in two phases: first outside-in and then inside-out. Initializer lists are executed outside-in (from derived class to base class). After this is done, the object’s members should be initialized, the object is considered to be “constructed”, and this exists. (That’s why you can’t use this in a meaningful way in an initializer … Read more

How to change status bar color in Flutter?

Update Flutter 2.0 (Recommended): On latest Flutter version, you should use: AppBar( systemOverlayStyle: SystemUiOverlayStyle( // Status bar color statusBarColor: Colors.red, // Status bar brightness (optional) statusBarIconBrightness: Brightness.dark, // For Android (dark icons) statusBarBrightness: Brightness.light, // For iOS (dark icons) ), ) Only Android (more flexibility): import ‘package:flutter/services.dart’; void main() { SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle( systemNavigationBarColor: Colors.blue, // navigation … 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)); }

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

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 to implement a main function in polymer apps

index.html <head> <!– <link rel=”import” href=”https://stackoverflow.com/questions/20982489/packages/polymer/polymer.html”> not necessary anymore (only in elements)–> <!– <script src=”packages/web_components/platform.js”></script> not necessary anymore with Polymer >= 0.14.0 –> <!– <script src=”packages/web_components/dart_support.js”></script> not necessary anymore with Polymer >= 0.15.0 –> <!– old –> <script type=”application/dart”> export ‘package:polymer/init.dart’;</script> <!– new –> <script type=”application/dart”>export ‘index.dart’;</script> </head> <body> … <!– … when you use … Read more