dart Dart Future API 3

Dart Future API 3

future_error_handling.dart
import 'dart:async';

const data = "I'm expansive data";

// Future with String data is being returned.
// This function returns the instance of
// Future and not the actual data.

Future<String> makeDataCall() async {
  var data = await getData();
  throw Exception("Error occurred in making data call");
}

String getData() {
  return data;
}

void main() {
  var theFurture = makeDataCall();
  theFurture.then((value) {
    print(value);
  }).catchError((error){
    print(error);
  });
}

dart Dart Future API 2

Dart Future API 2

future_two.dart
import 'dart:async';

const data = "I'm expansive data";

Future<void> makeDataCall() async {
  var data = await getData();
  print(data);
}

String getData() {
  return data;
}

void main() {
  var theFurture = makeDataCall();
  theFurture.then((_){
      print("There's nothing to be printed here. Work is already done.");
  });
}

dart Dart Future API

Dart Future API

future_one.dart
import 'dart:async';

const data = "I'm expansive data";

Future<String> makeDataCall() async {
  var data = await getData();
  return data;
}

String getData() {
  return data;
}

void main() {
  var theFurture = makeDataCall();
  theFurture.then((value){
      print(value);
  });
}

dart Dart Async和Await

Dart Async和Await

async_await_sequence.dart
import 'dart:async';

const data = "I'm expansive data";

void getDataA() {
  print("dataA");
}

void getDataB() {
  print("dataB");
}

String getDataC() {
  return "dataC";
}

void printMyData(String data) {
  print(data);
}

main() async {
  await getDataA();
  await getDataB();
  printMyData(await getDataC());
}

dart Dart Await和Async

Dart Await和Async

error_handling_future.dart
import 'dart:async';

const data = "I'm expansive data";

Future<void> makeDataCall() async {
  try {
    var data = await getData();
    throw Exception(
      "Error occurred in fetching data"
    );
  } catch(e) {
    print(e.toString());
  }
  
  
  print(data);
}

String getData() {
  return data;
}

void main(List<String> args) {
  makeDataCall();
}

dart Dart Await和Async

Dart Await和Async

await_async.dart
import 'dart:async';

const data = "I'm expansive data";

Future<void> makeDataCall() async {
  var data = await getData();
  print(data);
}

String getData() {
  return data;
}

void main(List<String> args) {
  makeDataCall();
}

dart 进度条颤振

ProgressBar.widget.dart
import 'package:flutter/material.dart';

/// Builds a progress bar with animated progress
class ProgressBar extends StatelessWidget {

  /// The height of the LinearGauge
  final double height;

  /// The progress of the bar in percentage and between 0 and 100
  int progress;

  double progressBarWidth;

  /// The padding around the bar
  final EdgeInsets padding;

  /// If we need to display the text beside the Bar
  final bool showText;

  /// The color of the bar and the text
  final Color color;

  ProgressBar({
    @required progress,
    this.height = 15.0,
    this.padding = const EdgeInsets.all(15.0),
    this.showText = true,
    this.color = Colors.blue
  }){
    // If the progress parameter is not between 0 and 100, we throw an exception
    if (progress >= 0 && progress <= 100) {
     this.progress = progress;
    } else {
     throw new FormatException("Progress must be equal or superior to 0 and equel or inferior to 100.");
    }
  }

  /// Render the progress part itself, called by the Build method which mostly contains the layout
  Widget buildProgressBar() {
    return Container(
      child: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          return Stack(
            children: <Widget>[

              // The background bar (full width)
              Container(
                width: constraints.maxWidth,
                child: Opacity(
                  opacity: 0.2,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular( this.height / 2 ),
                    child: Container(
                      height: this.height,
                      color: this.color,
                    ),
                  ),
                ),
              ),

              // The progression bar
              AnimatedContainer(
                duration: Duration(milliseconds : 200),
                curve: Curves.easeInOut,
                width: (this.progress > 0) ? this.progress * constraints.maxWidth / 100 : 0,
                child: ClipRRect(
                  borderRadius: BorderRadius.circular( this.height / 2 ),
                  child: Container(
                    height: this.height,
                    color: this.color,
                  ),
                ),
              )
            ],
          );
        },
      ),
    );

  }

  /// Returns a new instance of the ProgressBar with the progress parameter updated for using it on widgets
  Widget updateProgress(int progress) {
    if (progress > 0 && progress < 100) {
      return ProgressBar(
          height: this.height,
          progress: progress,
          padding: this.padding,
          showText: this.showText,
          color: this.color
      );
    } else if (progress != 100) {
      return ProgressBar(
          height: this.height,
          progress: 100,
          padding: this.padding,
          showText: this.showText,
          color: this.color
      );
    } else {
      return this;
    }
  }

  @override
  Widget build(BuildContext context) {
    if (this.showText) {
      return SingleChildScrollView(
        child: Container(
          padding: this.padding,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Expanded(
                  flex: 6,
                  child: this.buildProgressBar()
              ),

              Expanded(
                flex: 1,
                child: Text(
                  "${this.progress}%",
                  textAlign: TextAlign.right,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    color: this.color
                  )
                )
              )
            ],
          ),
        ),
      );
    } else {
      return Container(

        padding: this.padding,
        child: this.buildProgressBar()
      );
    }
  }
}

dart 变量是Dart中的对象

变量是Dart中的对象

variable_type.dart
void main() {
  int count = 5;
  if(count is Object) {
    print("Count is an object");
  } else {
    print("Count is not an object");
  }
}

dart 在Dart中异步和等待

在Dart中异步和等待

async.dart
void main() {
    var testAsync = TestAsync();
    testAsync.printWithDelay("Hello Async Dart!");
}

class TestAsync {
  static const fiveSecond = Duration(seconds: 5);

  Future<void> printWithDelay(String message) async {
    await Future.delayed(fiveSecond);
    print(message);
  }  
}

dart Dart中的Mixins

Dart中的Mixins

mixins.dart
void main() {
  print("Hello World!");
  var dog = Dog("Aung Net", "red");
  dog.eat();
  dog.bark();
  print(dog.legs);
  dog.runWithFourLegs();
}

class Canis {
  int legs = 4;
  void runWithFourLegs() {
    print("Run with 4 Legs");
  }

}

class Animal {
  String name;
  String color;
  Animal(this.name, this.color);

  void eat() {
    print("EAT!");
  }
}

class Dog extends Animal with Canis {
  Dog(String name, String color) : super(name, color);

  void bark() {
    print("WOOF!");
  }
}