Skip to content

Instantly share code, notes, and snippets.

View gauravbharat's full-sized avatar

Gaurav Bhārat Mendse gauravbharat

View GitHub Profile
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" ],
@gauravbharat
gauravbharat / extension.dart
Created February 23, 2021 13:53
Dart: mixins and extensions
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);
}
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);
@gauravbharat
gauravbharat / abstract_class.dart
Last active March 4, 2021 02:15
classes in dart
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;
@gauravbharat
gauravbharat / filter_list_generic.dart
Last active March 4, 2021 02:16
Dart: Implementing where and firstWhere using generics
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);
@gauravbharat
gauravbharat / futures_async_await.dart
Created November 5, 2020 11:40
Futures with async and await
/* 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'));
}
@gauravbharat
gauravbharat / iterables.dart
Created November 5, 2020 10:55
Some examples on Dart iterable and its methods
/* 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) {
@gauravbharat
gauravbharat / generators.dart
Created November 4, 2020 13:21
Synchronous and Asynchronous Generator Functions
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* {
@gauravbharat
gauravbharat / streams.dart
Created November 3, 2020 13:12
Streams example created out of Flutter videos from YouTube
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;
@gauravbharat
gauravbharat / futures.dart
Created November 3, 2020 12:55
Dart Futures example from Flutter videos on YouTube
// 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))