“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Hi 👋

  • Wechat: RyukieW
  • Wechat official account: LabLawliet
  • 📦 Archive of technical articles
  • 🐙 making
My personal project Minesweeper Elic Endless Ladder Dream of books Privacy Access Record
type The game financial tool
AppStore Elic Umemi Privacy Access Record

More columns:

The independent development of Lawliet

Lawliet’s iOS garden party

Lawliet’s underlying iOS lab

Lawliet’s iOS Reverse lab

Lawliet’s brush book

preface

We discussed the use of assertions on the contact page in the previous article on the various Types of Cells for More tips on Flutter development.

Initialization method overrides

We can override initState() for a StatefulWidget if we need to do something with it during initialization.

class _ContactsPageState extends State<ContactsPage> {

  @override
  void initState() {
    super.initState();

    // Add data_contacts .addAll(friendsData); _systems .addAll(friendsHeaderData); }... }Copy the code

Chain calls

In iOS development, we often use chain calls like this when we use SnapKit for UI control constraint layout, which is very cool.

make.leading.trailing.top.equalToSuperview()
Copy the code

We can also use a similar method for Flutter development:

@override
  void initState() {
    super.initState();

    // chain calls to add elements multiple times_contacts .. addAll(friendsData) .. addAll(friendsData); _systems.addAll(friendsHeaderData); }Copy the code

The sorting

It can be seen above.. AddAll (friendsData) is called consecutively. So could more be done?

Normally we would sort the contact list alphabetically. Here we try using a chain call:

@override
  void initState() {
    super.initState(); _contacts .. addAll(friendsData) .. addAll(friendsData) .. sort((a, b) {if (a.indexLetter == null || b.indexLetter == null) {
          return 0;
        }
        returna.indexLetter! .compareTo(b.indexLetter!) ; }); _systems.addAll(friendsHeaderData); }Copy the code

Different from Swift sort

Here you might notice that sort is a little different from Swift in that the internal callback returns an int instead of a bool.

We can learn more with the function header comment:

  /// Sorts this list according to the order specified by the [compare] function.
  ///
  /// The [compare] function must act as a [Comparator].
  /// ```dart
  /// var numbers = ['two', 'three', 'four'];
  /// // Sort from shortest to longest.
  /// numbers.sort((a, b) => a.length.compareTo(b.length));
  /// print(numbers); // [two, four, three]
  /// ` ` `
  /// The default [List] implementations use [Comparable.compare] if
  /// [compare] is omitted.
  /// ```dart
  /// List<int> nums = [13, 2, -11];
  /// nums.sort();
  /// print(nums); / / / - 11, 2, 13
  /// ` ` `
  /// In that case, the elements of the list must be [Comparable] to
  /// each other.
  ///
  /// A [Comparator] may compare objects as equal (return zero), even if they
  /// are distinct objects.
  /// The sort function is not guaranteed to be stable, so distinct objects
  /// that compare as equal may occur in any order in the result:
  /// ```dart
  /// var numbers = ['one', 'two', 'three', 'four'];
  /// numbers.sort((a, b) => a.length.compareTo(b.length));
  /// print(numbers); // [one, two, four, three] OR [two, one, four, three]
  /// ` ` `
  void sort([intcompare(E a, E b)? ] );Copy the code

A brief writing

The sorting function here can be abbreviated under relatively simple conditions to improve coding efficiency

Not short

sort((a, b) {
  if (a.indexLetter == null || b.indexLetter == null) {
    return 0;
  }
  returna.indexLetter! .compareTo(b.indexLetter!) ; });Copy the code

shorthand

sort( (a, b) => a.indexLetter.compareTo(b.indexLetter) );
Copy the code

Header file comments have such shorthand instructions, usually read more comments, can help us write more language style of code oh ~