This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mediaJSON = { "categories" : [ { "name" : "Movies", | |
"videos" : [ | |
{ "description" : "Big Buck Bunny tells the story of a giant rabbit with a heart bigger than himself. When one sunny day three rodents rudely harass him, something snaps... and the rabbit ain't no bunny anymore! In the typical cartoon tradition he prepares the nasty rodents a comical revenge.\n\nLicensed under the Creative Commons Attribution license\nhttp://www.bigbuckbunny.org", | |
"sources" : [ "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" ], | |
"subtitle" : "By Blender Foundation", | |
"thumb" : "images/BigBuckBunny.jpg", | |
"title" : "Big Buck Bunny" | |
}, | |
{ "description" : "The first Blender Open Movie from 2006", | |
"sources" : [ "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4" ], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension IterableX<T extends num> on Iterable<T> { | |
T sum() => reduce((value, element) => (value + element) as T); | |
} | |
void main() { | |
final sum = [1.0, 2.0, 3.0].sum(); | |
print(sum); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
Person({required this.name, required this.age}); | |
final String name; | |
final int age; | |
factory Person.fromJSON(Map<String, Object> json) { | |
final name = json['name']; | |
final age = json['age']; | |
if (name is String && age is int) { | |
return Person(name: name, age: age); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
/// Code with abstractions, to be independant from specific implementations | |
/// We can use abstract classes to define an interface that can be implemented by subclasses | |
/// Very powerful: decouples code that uses an interface from its implementation | |
/// Abstract classes can't be instantiated like concrete classes, but its subclasses can be | |
abstract class Shape { | |
double get area; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
const List<int> list = [1, 2, 3, 4]; | |
final odd = where<int>(list, (value) => value % 2 == 0); | |
final even = where<int>(list, (value) => value % 2 != 0); | |
print(odd); | |
print(even); | |
final result = firstWhere(list, (x) => x == 4, orElse: () => -1); | |
print(result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Key terms: | |
* synchronous operation: A synchronous operation blocks other operations from executing until it completes. | |
* synchronous function: A synchronous function only performs synchronous operations. | |
* asynchronous operation: Once initiated, an asynchronous operation allows other operations to execute before it completes. | |
* asynchronous function: An asynchronous function performs at least one asynchronous operation and can also perform synchronous operations. */ | |
Future<void> fetchUserOrder1() { | |
return Future.delayed(Duration(seconds: 2), () => print('Large Latte')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Predicate: A function that returns true when a certain condition is satisfied. */ | |
bool predicate(String el) { | |
return el.length > 5; | |
} | |
void main() { | |
print('*** Iterable and its methods ***'); | |
// Loop through an iterable to print values | |
var iterable = ['Salad', 'Popcorn', 'Toast']; | |
for (var element in iterable) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
// Synchronous Generator Functions | |
// Declare a return type of Iterable | |
// sync* keyword to mark the function as a synchronous generator, just a way to tell dart | |
// that this function is going to generate multiple values on-demand | |
// yield keyword is like a return, but it does not end the function. | |
// instead it returns a single value and wait for the caller to request the next | |
Iterable<int> getRange(int start, int finish) sync* { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
// Create a stream of numbers | |
class NumberCreator { | |
var _count = 1; | |
// Create a stream controller | |
final _controller = StreamController<int>(); | |
Stream<int> get stream => _controller.stream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Futures example from Flutter videos on YouTube | |
import 'dart:async'; | |
void main() { | |
Future<int>.delayed( | |
Duration(seconds: 3), | |
() { return 100; } | |
) | |
.then((data) => data + 200) | |
.then((val) => print(val)) |
NewerOlder