Dart Generators & Callable Class In Flutter

Shaiq khan
Aug 10 · 5 min read
Image for post
Image for post

Table of Content :



Dart Generators

Synchronous Generator

import 'package:flutter/material.dart';void main() {
print("Iterable [sync* + yield]");
Iterable<int> sequence = countDown(3);
print("CountDown Start");for (int value in sequence) {
print(value);
}
print("Complete");
}
Iterable<int> countDown(int num) sync* {
while (num > 0) {
yield num--;
}
}

Output :

Image for post
Image for post

Asynchronous Generator

import 'package:flutter/material.dart';void main() {
print("Stream [async* + yield]");
Stream<int> sequence = countDown(3);
print("CountDown Start");sequence.listen((int value) {
print(value);
});
print("Complete");
}
Stream<int> countDown(int num) async* {
while (num > 0) {
yield num--;
}
}

Output:

Image for post
Image for post

Recursive Synchronous Generator

import 'package:flutter/material.dart';void main() {
print("Iterable [sync* + yield*]");
Iterable<int> sequence = countDownRecursive(4);
print("CountDown Start");for (int value in sequence) {
print(value);
}
print("Complete");
}
Iterable<int> countDownRecursive(int num) sync* {
if (num > 0) {
yield num;
yield* countDownRecursive(num -1);
}
}
Image for post
Image for post

Recursive Asynchronous Generator

import 'package:flutter/material.dart';void main() {
print("Stream [async* + yield*]");
Stream<int> sequence = countDownRecursive(4);
print("CountDown Start");sequence.listen((int value) {
print(value);
});
print("Complete");
}
Stream<int> countDownRecursive(int num) async* {
if (num > 0) {
yield num;
yield* countDownRecursive(num -1);
}
}
Image for post
Image for post

Callable Class

Implement Callable Class

class Multiply {
int call(int a, int b) => a * b;
}

Using Callable Class

void main() {
Multiply multiply = Multiply();
var result = multiply(2, 2);
print(result mpy);
}
I/flutter (25099): 4

Conclusion:



Image for post
Image for post

FlutterDevs

FlutterDevs intent to deliver Flutter apps with high…

Shaiq khan

Written by

Flutter Developer

FlutterDevs

FlutterDevs intent to deliver Flutter apps with high quality. We’ve adopted Design First attitude which helps us deliver applications of highest quality.

Shaiq khan

Written by

Flutter Developer

FlutterDevs

FlutterDevs intent to deliver Flutter apps with high quality. We’ve adopted Design First attitude which helps us deliver applications of highest quality.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store