Skip to content

Latest commit

 

History

History
190 lines (139 loc) · 4.5 KB

Flutter Notes.md

File metadata and controls

190 lines (139 loc) · 4.5 KB

Flutter Notes

Operators

First Lesson of Reso Coder Free Course

Spread

Joining Lists without Spread Operator

List<int> joinCollectionsWithoutSpread() {
  final collection = [1, 2, 3];
  final otherCollection = [4, 5, 6];
  collection.addAll(otherCollection);
  return collection;
}

Joining Lists with Spread Operator

List<int> joinCollectionsWithSpread() {
  final collection = [1, 2, 3];
  final otherCollection = [4, 5, 6];
  return [...collection, ...otherCollection];
}

Spread can be used like this...

ListView(
      children: <Widget>[
        Text('Fake email input'),
        Text('Fake password input'),
        if (showLoginUI) ...[
          RaisedButton(
            child: Text('Login'),
            onPressed: () {},
          ),
          FlatButton(
            child: Text('Forgotten Password'),
            onPressed: () {},
          ),
        ],
        if (!showLoginUI)
          RaisedButton(
            child: Text('Register'),
            onPressed: () {},
          ),
      ],
);
Cascade

Without Using Cascade...

List<int> withoutCascade() {
  final list = [5, 1, 3, 2, 4];
  list.sort();
  list.removeLast();
  return list;
}

With Using Cascade..

List<int> withCascade() {
  return [5, 1, 3, 2, 4]
    ..sort()
    ..removeLast();
}
Nullability
import 'package:flutter/material.dart';

void withoutCoalescing() {
  String myValue;
  String fallbackValue = 'fallback';

  String result;
  if (myValue == null)
    result = fallbackValue;
  else
    result = myValue;

  final resultTernary = myValue == null ? fallbackValue : myValue;

  print(result);
  print(resultTernary);
}

void withCoalescing() {
  String myValue;
  String fallbackValue = 'fallback';

  final result = myValue ?? fallbackValue;
  print(result);
}

void assignment() {
  String uninitializedValue;
  String initializedValue = 'something';

  uninitializedValue ??= 'new value';
  initializedValue ??= 'new value';

  print(uninitializedValue);
  print(initializedValue);
}

void access() {
  String uninitializedValue;
  String initializedValue = 'something';

  print(uninitializedValue?.toUpperCase());
  print(initializedValue?.toUpperCase());
}

void spread() {
  List<int> collection;
  final otherCollection = [4, 5, 6];
  final result = [...?collection, ...?otherCollection];
  print(result);
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // withoutCoalescing();
    // withCoalescing();
    // assignment();
    // access();
    spread();
    return Container();
  }
}

Solutions of Problems

-You know what? I will try to implement something with agora.

Resources