Skip to content
Useful Flutter and Dart Tips.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
tips inkwell snippet file Apr 29, 2019
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md Mar 26, 2019
CONTRIBUTING.md Create CONTRIBUTING.md Mar 26, 2019
LICENSE Initial commit Jan 14, 2019
README.md Tip: 11. Using Plurals in your Dart String. May 14, 2019
_config.yml Set theme jekyll-theme-cayman Apr 23, 2019
main.dart Create main.dart May 4, 2019

README.md

11. Using Plurals in your Dart String.

Plurals: Different languages have different rules for grammatical agreement with quantity. In English, for example, the quantity 1 is a special case. We write "1 book", but for any other quantity we'd write "n books". This distinction between singular and plural is very common, but other languages make finer distinctions.

You can use Plurals in your Dart string by using Intl package. The full set supported by Intl package is zero, one, two, few, many, and other.

  • Add dependency:
dependencies:
  intl: version
  • How to use:
import 'package:intl/intl.dart';
...
notificationCount(int howMany) => Intl.plural(
      howMany,
      zero: 'You don\'t have any notification.',
      one: 'You have $howMany notification.',
      other: 'You have $howMany notifications.',
      name: "notification",
      args: [howMany],
      examples: const {'howMany': 42},
      desc: "How many notifications are there.",
    );

    print(notificationCount(0));
    print(notificationCount(1));
    print(notificationCount(2));
    
  • Output:
You don't have any notification.
You have 1 notification.
There are 2 notifications.

10. Having trouble displaying splashes using an InkWell?

Use an Ink widget! The Ink widget draws on the same widget that InkWell does, so the splash appears. #FlutterFriday tweet by Flutter.dev.

Learn more here.

Screenshot

9. Want to log data on the system console in Flutter?

You can use the print() function to view it in the system console. If your output is too much, then Android sometimes discards some log lines. To avoid this, you can use debugPrint().

You can also log your print calls to disk if your doing long-term or background work.

Check out this Gist by Simon Lightfoot

8. Cascade Notation - Method Chaining on Steroids 💊💉

Cascades Notation (..) allows to chain a sequence of operations on same object. In addition, fields (data-memebers) can be accessed using the same.

Open in DartPad 🎯

Screenshot

7. Want to set different Theme for a perticular widget ?

Just wrap the widget with the Theme Widget and pass the ThemeData().

Screenshot

Monthly Tip Article (February 2019)

This article contains the Tips from February month that shared over here.

#2 Flutter + Dart Tips

6. Use Ternary operator instead of the if else to shorter your Dart code.

Use below.

Screenshot

Instead of this.

Screenshot

5. Want to run task periodically in Dart?

What about using Timer.periodic It will create a repeating timer, It will take a two argument one is duration and second is callback that must take a one Timer parameter.

Screenshot

You can cancle the timer using the timer.cancel().

4. Apply style as a Theme in a Text widget.

Check out below article for detail information about this tip. Apply style as a Theme in a Text widget

Screenshot

Monthly Tip Article (January 2019)

This article contains the Tips from January month that shared over here.

#1 Flutter + Dart Tips

3. Do not explicitly initialize variables to null.

Adding = null is redundant and unneeded.

Screenshot

2. Using ListView.separated()

Want to add the separator in your Flutter ListView?

Go for the ListView.separated();

Best part about seprated is it can be any widget.😃

Check out the below image for the sample code.

Screenshot

1. Using null-aware operators

While checking the null in the Dart, Use null-aware operators help you reduce the amount of code required to work with references that are potentially null.

Screenshot

You can’t perform that action at this time.