Slide 1

Slide 1 text

Flutter at scale @jcocaramos - Droidcon NYC 2019

Slide 2

Slide 2 text

Before we start Our Journey to Flutter - Android Summit 2019 - @jcocaramos - Droidcon NYC 2019

Slide 3

Slide 3 text

What do you mean by Flutter at scale? @jcocaramos - Droidcon NYC 2019

Slide 4

Slide 4 text

Complex problems, easy solutions. @jcocaramos - Droidcon NYC 2019

Slide 5

Slide 5 text

@jcocaramos - Droidcon NYC 2019

Slide 6

Slide 6 text

Goals before code @jcocaramos - Droidcon NYC 2019

Slide 7

Slide 7 text

Business and design goals We want to be able to regularly release our products across all brands, all platforms, and all regions simultaneously, with the same feature capabilities @jcocaramos - Droidcon NYC 2019

Slide 8

Slide 8 text

Engineering goals Create a platform that is developer friendly, developer scalable, and performant, which provides safe experimentation and continuos deployment. @jcocaramos - Droidcon NYC 2019

Slide 9

Slide 9 text

@jcocaramos - Droidcon NYC 2019

Slide 10

Slide 10 text

Release regularly → Train releases: every X weeks, whether you are ready or not → Trunk-based development: all code is on master @jcocaramos - Droidcon NYC 2019

Slide 11

Slide 11 text

All brands, regions, and platforms → BMW, BMWi, MINI, Rolls Royce → Global presence, but different legal/market requirements → One single Flutter codebase, multiple apps with different design libraries @jcocaramos - Droidcon NYC 2019

Slide 12

Slide 12 text

Same feature capabilities → Feature flags to support different integrations, markets, and configurations → Cloud centric: reduce number of decisions on mobile, it is just a rendering client @jcocaramos - Droidcon NYC 2019

Slide 13

Slide 13 text

Developer friendly → Tests. Tons of tests. → Consistent coding style → Tech debt is addressed soon @jcocaramos - Droidcon NYC 2019

Slide 14

Slide 14 text

Developer scalable → Assume 1 to N developers → Natural onboarding → Keep it simple! @jcocaramos - Droidcon NYC 2019

Slide 15

Slide 15 text

Performant → 60fps → 99.9% crash free → Measure, analyze, improve → Tests, tests, tests → Easy to identify where are the problems @jcocaramos - Droidcon NYC 2019

Slide 16

Slide 16 text

Safe experimentation → Feature flags, A/B → Complexity out of the client → Predictable APIs, natural language → Variants are forced to be tested on integration @jcocaramos - Droidcon NYC 2019

Slide 17

Slide 17 text

Continuos deployment → Merge a commit, you are in production! → Same philosophy on backend and client → Percentage rollout → Monitoring, alerting, and remote debugging @jcocaramos - Droidcon NYC 2019

Slide 18

Slide 18 text

@jcocaramos - Droidcon NYC 2019

Slide 19

Slide 19 text

How do we do this with Flutter? @jcocaramos - Droidcon NYC 2019

Slide 20

Slide 20 text

Learn from the past @jcocaramos - Droidcon NYC 2019

Slide 21

Slide 21 text

Our tools → High level architecture → flutter_bloc (@felangelov) → lumberdash (@bmw-tech) → unit, widget, and integration tests → ozzie (@bmw-tech) @jcocaramos - Droidcon NYC 2019

Slide 22

Slide 22 text

Example Find more at felangel.github.io/ bloc @jcocaramos - Droidcon NYC 2019

Slide 23

Slide 23 text

High level architecture → Vertical modules (data, domain, UI & state) → Horizontal modules (feature composition) @jcocaramos - Droidcon NYC 2019

Slide 24

Slide 24 text

Flutter is composable by nature @jcocaramos - Droidcon NYC 2019

Slide 25

Slide 25 text

flutter_bloc A predictable state management library that helps implement the BLoC design pattern @jcocaramos - Droidcon NYC 2019

Slide 26

Slide 26 text

UI = f (state) @jcocaramos - Droidcon NYC 2019

Slide 27

Slide 27 text

UI = f (state) → UI is the layout of the screen → f is the build method of your widgets → state is the different scenarios that your application can handle @jcocaramos - Droidcon NYC 2019

Slide 28

Slide 28 text

States Pure Dart classes used to describe a particular moment in the life cycle of the application. States can be tied to a UI screen (counter value) or not (user is authenticated) int counter; @jcocaramos - Droidcon NYC 2019

Slide 29

Slide 29 text

UI Events Pure Dart classes (POJOs) used to describe actions triggered from the system/user enum CounterEvent { increment, decrement, } @jcocaramos - Droidcon NYC 2019

Slide 30

Slide 30 text

bloc Pure Dart class that receives events, and produces states. → Can be used in other Dart projects as well (Angular, CLI...) → Takes repositories as dependencies → Provides an initialState and a mapEventToState @jcocaramos - Droidcon NYC 2019

Slide 31

Slide 31 text

flutter_bloc → Flutter widgets to build, provide, and manage bloc dependencies in the widget tree → There's a version for AngularDart @jcocaramos - Droidcon NYC 2019

Slide 32

Slide 32 text

class CounterBloc extends Bloc { @override int get initialState => 0; @override Stream mapEventToState(CounterEvent event) async* { switch (event) { case CounterEvent.decrement: yield currentState - 1; break; case CounterEvent.increment: yield currentState + 1; break; } } } @jcocaramos - Droidcon NYC 2019

Slide 33

Slide 33 text

class CounterBloc extends Bloc { @override int get initialState => 0; @override Stream mapEventToState(CounterEvent event) async* { switch (event) { case CounterEvent.decrement: yield currentState - 1; break; case CounterEvent.increment: yield currentState + 1; break; } } } @jcocaramos - Droidcon NYC 2019

Slide 34

Slide 34 text

class CounterBloc extends Bloc { @override int get initialState => 0; @override Stream mapEventToState(CounterEvent event) async* { switch (event) { case CounterEvent.decrement: yield currentState - 1; break; case CounterEvent.increment: yield currentState + 1; break; } } } @jcocaramos - Droidcon NYC 2019

Slide 35

Slide 35 text

class CounterBloc extends Bloc { @override int get initialState => 0; @override Stream mapEventToState(CounterEvent event) async* { switch (event) { case CounterEvent.decrement: yield currentState - 1; break; case CounterEvent.increment: yield currentState + 1; break; } } } @jcocaramos - Droidcon NYC 2019

Slide 36

Slide 36 text

Flutter class CounterApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', home: BlocProvider( builder: (context) => CounterBloc(), child: CounterPage(), ), ); } } @jcocaramos - Droidcon NYC 2019

Slide 37

Slide 37 text

Flutter class CounterApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', home: BlocProvider( builder: (context) => CounterBloc(), child: CounterPage(), ), ); } } @jcocaramos - Droidcon NYC 2019

Slide 38

Slide 38 text

class CounterPage extends StatelessWidget { @override Widget build(BuildContext context) { final CounterBloc counterBloc = BlocProvider.of(context); return Scaffold( appBar: AppBar(title: Text('Counter')), body: BlocBuilder( builder: (context, count) { return Text('$count'); }, ), floatingActionButton: Column( children: [ FloatingActionButton( child: Icon(Icons.add), onPressed: () { counterBloc.dispatch(CounterEvent.increment); }, ) FloatingActionButton( child: Icon(Icons.remove), onPressed: () { counterBloc.dispatch(CounterEvent.decrement); }, ), ], ), ); } } @jcocaramos - Droidcon NYC 2019

Slide 39

Slide 39 text

class CounterPage extends StatelessWidget { @override Widget build(BuildContext context) { final CounterBloc counterBloc = BlocProvider.of(context); return Scaffold( appBar: AppBar(title: Text('Counter')), body: BlocBuilder( builder: (context, count) { return Text('$count'); }, ), floatingActionButton: Column( children: [ FloatingActionButton( child: Icon(Icons.add), onPressed: () { counterBloc.dispatch(CounterEvent.increment); }, ) FloatingActionButton( child: Icon(Icons.remove), onPressed: () { counterBloc.dispatch(CounterEvent.decrement); }, ), ], ), ); } } @jcocaramos - Droidcon NYC 2019

Slide 40

Slide 40 text

class CounterPage extends StatelessWidget { @override Widget build(BuildContext context) { final CounterBloc counterBloc = BlocProvider.of(context); return Scaffold( appBar: AppBar(title: Text('Counter')), body: BlocBuilder( builder: (context, count) { return Text('$count'); }, ), floatingActionButton: Column( children: [ FloatingActionButton( child: Icon(Icons.add), onPressed: () { counterBloc.dispatch(CounterEvent.increment); }, ) FloatingActionButton( child: Icon(Icons.remove), onPressed: () { counterBloc.dispatch(CounterEvent.decrement); }, ), ], ), ); } } @jcocaramos - Droidcon NYC 2019

Slide 41

Slide 41 text

class CounterPage extends StatelessWidget { @override Widget build(BuildContext context) { final CounterBloc counterBloc = BlocProvider.of(context); return Scaffold( appBar: AppBar(title: Text('Counter')), body: BlocBuilder( builder: (context, count) { return Text('$count'); }, ), floatingActionButton: Column( children: [ FloatingActionButton( child: Icon(Icons.add), onPressed: () { counterBloc.dispatch(CounterEvent.increment); }, ) FloatingActionButton( child: Icon(Icons.remove), onPressed: () { counterBloc.dispatch(CounterEvent.decrement); }, ), ], ), ); } } @jcocaramos - Droidcon NYC 2019

Slide 42

Slide 42 text

class CounterPage extends StatelessWidget { @override Widget build(BuildContext context) { final CounterBloc counterBloc = BlocProvider.of(context); return Scaffold( appBar: AppBar(title: Text('Counter')), body: BlocBuilder( builder: (context, count) { return Text('$count'); }, ), floatingActionButton: Column( children: [ FloatingActionButton( child: Icon(Icons.add), onPressed: () { counterBloc.dispatch(CounterEvent.increment); }, ) FloatingActionButton( child: Icon(Icons.remove), onPressed: () { counterBloc.dispatch(CounterEvent.decrement); }, ), ], ), ); } } @jcocaramos - Droidcon NYC 2019

Slide 43

Slide 43 text

class CounterPage extends StatelessWidget { @override Widget build(BuildContext context) { final CounterBloc counterBloc = BlocProvider.of(context); return Scaffold( appBar: AppBar(title: Text('Counter')), body: BlocBuilder( builder: (context, count) { return Text('$count'); }, ), floatingActionButton: Column( children: [ FloatingActionButton( child: Icon(Icons.add), onPressed: () { counterBloc.dispatch(CounterEvent.increment); }, ) FloatingActionButton( child: Icon(Icons.remove), onPressed: () { counterBloc.dispatch(CounterEvent.decrement); }, ), ], ), ); } } @jcocaramos - Droidcon NYC 2019

Slide 44

Slide 44 text

Easy, right? @jcocaramos - Droidcon NYC 2019

Slide 45

Slide 45 text

Analytics and remote debugging, 100% free @jcocaramos - Droidcon NYC 2019

Slide 46

Slide 46 text

lumberdash Do you need logs? Lumberdash is the answer! void main() { putLumberdashToWork(withClient: SimpleClient()); logWarning('Hello Warning'); logFatal('Hello Fatal!'); logMessage('Hello Message!'); logError(Exception('Hello Error')); } @jcocaramos - Droidcon NYC 2019

Slide 47

Slide 47 text

lumberdash Do you need logs? Lumberdash is the answer! void main() { putLumberdashToWork(withClient: SimpleClient()); logWarning('Hello Warning'); logFatal('Hello Fatal!'); logMessage('Hello Message!'); logError(Exception('Hello Error')); } @jcocaramos - Droidcon NYC 2019

Slide 48

Slide 48 text

lumberdash Do you need logs? Lumberdash is the answer! void main() { putLumberdashToWork(withClient: SimpleClient()); logWarning('Hello Warning'); logFatal('Hello Fatal!'); logMessage('Hello Message!'); logError(Exception('Hello Error')); } @jcocaramos - Droidcon NYC 2019

Slide 49

Slide 49 text

Open Source Lumberdash clients → SimpleClient → ColorizeClient → SentryClient → FirebaseClient @jcocaramos - Droidcon NYC 2019

Slide 50

Slide 50 text

if (isDebug) { putLumberdashToWork(withClient: SimpleClient()); } else { putLumberdashToWork(withClient: FirebaseClient()); } logMessage('Hello Message!'); @jcocaramos - Droidcon NYC 2019

Slide 51

Slide 51 text

flutter_bloc ❤ lumberdash class CounterBlocDelegate extends BlocDelegate { final LumberdashClient lumberdashClient; CounterBlocDelegate(this.lumberdashClient); @override void onTransition(Transition transition) { super.onTransition(transition); lumberdashClient.logMessage(transition.toString()); } @override void onError(Object error, StackTrace stacktrace) { super.onError(error, stacktrace); lumberdashClient.logError(error, stacktrace); } } @jcocaramos - Droidcon NYC 2019

Slide 52

Slide 52 text

flutter_bloc ❤ lumberdash class CounterBlocDelegate extends BlocDelegate { final LumberdashClient lumberdashClient; CounterBlocDelegate(this.lumberdashClient); @override void onTransition(Transition transition) { super.onTransition(transition); lumberdashClient.logMessage(transition.toString()); } @override void onError(Object error, StackTrace stacktrace) { super.onError(error, stacktrace); lumberdashClient.logError(error, stacktrace); } } @jcocaramos - Droidcon NYC 2019

Slide 53

Slide 53 text

flutter_bloc ❤ lumberdash class CounterBlocDelegate extends BlocDelegate { final LumberdashClient lumberdashClient; CounterBlocDelegate(this.lumberdashClient); @override void onTransition(Transition transition) { super.onTransition(transition); lumberdashClient.logMessage(transition.toString()); } @override void onError(Object error, StackTrace stacktrace) { super.onError(error, stacktrace); lumberdashClient.logError(error, stacktrace); } } @jcocaramos - Droidcon NYC 2019

Slide 54

Slide 54 text

void main() { BlocSupervisor.delegate = CounterBlocDelegate( lumberdashClient: lumberdashClient, ); runApp(CounterApp()); } @jcocaramos - Droidcon NYC 2019

Slide 55

Slide 55 text

Feature development is With these two simple tools, our feature/market teams can develop features with a consistent pattern, that is well documented, 100% testable, and provides automation for logs and analytics. @jcocaramos - Droidcon NYC 2019

Slide 56

Slide 56 text

@jcocaramos - Droidcon NYC 2019

Slide 57

Slide 57 text

What do we do in the core team to ensure quality? @jcocaramos - Droidcon NYC 2019

Slide 58

Slide 58 text

Tests, automation, and pipelines → Unit and widget tests with 100% coverage → Integration tests to ensure 60fps at all times → Visual dependency graph with validation → Consistent lint and code formatting in every module → Project generators with Docker and pipelines @jcocaramos - Droidcon NYC 2019

Slide 59

Slide 59 text

Pipeline @jcocaramos - Droidcon NYC 2019

Slide 60

Slide 60 text

Ozzie With Ozzie, during integration tests, we can capture screenshots of every frame we want, and we can also measure the performance of a feature, breaking the build if necessary @jcocaramos - Droidcon NYC 2019

Slide 61

Slide 61 text

Ozzie @jcocaramos - Droidcon NYC 2019

Slide 62

Slide 62 text

void main() { FlutterDriver driver; Ozzie ozzie; setUpAll(() async { driver = await FlutterDriver.connect(); ozzie = Ozzie.initWith(driver, groupName: 'counter'); }); tearDownAll(() async { if (driver != null) driver.close(); ozzie.generateHtmlReport(); }); test('initial counter is 0', () async { await ozzie.profilePerformance('counter0', () async { await driver.waitFor(find.text('0')); await ozzie.takeScreenshot('initial_counter_is_0'); await driver.tap(find.byType('FloatingActionButton')); await driver.waitFor(find.text('1')); await ozzie.takeScreenshot('counter_is_1'); }); }); } @jcocaramos - Droidcon NYC 2019

Slide 63

Slide 63 text

void main() { FlutterDriver driver; Ozzie ozzie; setUpAll(() async { driver = await FlutterDriver.connect(); ozzie = Ozzie.initWith(driver, groupName: 'counter'); }); tearDownAll(() async { if (driver != null) driver.close(); ozzie.generateHtmlReport(); }); test('initial counter is 0', () async { await ozzie.profilePerformance('counter0', () async { await driver.waitFor(find.text('0')); await ozzie.takeScreenshot('initial_counter_is_0'); await driver.tap(find.byType('FloatingActionButton')); await driver.waitFor(find.text('1')); await ozzie.takeScreenshot('counter_is_1'); }); }); } @jcocaramos - Droidcon NYC 2019

Slide 64

Slide 64 text

void main() { FlutterDriver driver; Ozzie ozzie; setUpAll(() async { driver = await FlutterDriver.connect(); ozzie = Ozzie.initWith(driver, groupName: 'counter'); }); tearDownAll(() async { if (driver != null) driver.close(); ozzie.generateHtmlReport(); }); test('initial counter is 0', () async { await ozzie.profilePerformance('counter0', () async { await driver.waitFor(find.text('0')); await ozzie.takeScreenshot('initial_counter_is_0'); await driver.tap(find.byType('FloatingActionButton')); await driver.waitFor(find.text('1')); await ozzie.takeScreenshot('counter_is_1'); }); }); } @jcocaramos - Droidcon NYC 2019

Slide 65

Slide 65 text

void main() { FlutterDriver driver; Ozzie ozzie; setUpAll(() async { driver = await FlutterDriver.connect(); ozzie = Ozzie.initWith(driver, groupName: 'counter'); }); tearDownAll(() async { if (driver != null) driver.close(); ozzie.generateHtmlReport(); }); test('initial counter is 0', () async { await ozzie.profilePerformance('counter0', () async { await driver.waitFor(find.text('0')); await ozzie.takeScreenshot('initial_counter_is_0'); await driver.tap(find.byType('FloatingActionButton')); await driver.waitFor(find.text('1')); await ozzie.takeScreenshot('counter_is_1'); }); }); } @jcocaramos - Droidcon NYC 2019

Slide 66

Slide 66 text

ozzie.yaml integration_test_expectations: should_fail_build_on_warning: true should_fail_build_on_error: true performance_metrics: missed_frames_threshold: warning_percentage: 5.0 error_percentage: 10.0 frame_build_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 frame_rasterizer_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 @jcocaramos - Droidcon NYC 2019

Slide 67

Slide 67 text

ozzie.yaml integration_test_expectations: should_fail_build_on_warning: true should_fail_build_on_error: true performance_metrics: missed_frames_threshold: warning_percentage: 5.0 error_percentage: 10.0 frame_build_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 frame_rasterizer_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 @jcocaramos - Droidcon NYC 2019

Slide 68

Slide 68 text

ozzie.yaml integration_test_expectations: should_fail_build_on_warning: true should_fail_build_on_error: true performance_metrics: missed_frames_threshold: warning_percentage: 5.0 error_percentage: 10.0 frame_build_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 frame_rasterizer_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 @jcocaramos - Droidcon NYC 2019

Slide 69

Slide 69 text

ozzie.yaml integration_test_expectations: should_fail_build_on_warning: true should_fail_build_on_error: true performance_metrics: missed_frames_threshold: warning_percentage: 5.0 error_percentage: 10.0 frame_build_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 frame_rasterizer_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 @jcocaramos - Droidcon NYC 2019

Slide 70

Slide 70 text

ozzie.yaml integration_test_expectations: should_fail_build_on_warning: true should_fail_build_on_error: true performance_metrics: missed_frames_threshold: warning_percentage: 5.0 error_percentage: 10.0 frame_build_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 frame_rasterizer_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 @jcocaramos - Droidcon NYC 2019

Slide 71

Slide 71 text

ozzie.yaml integration_test_expectations: should_fail_build_on_warning: true should_fail_build_on_error: true performance_metrics: missed_frames_threshold: warning_percentage: 5.0 error_percentage: 10.0 frame_build_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 frame_rasterizer_rate_threshold: warning_time_in_millis: 14.0 error_time_in_millis: 16.0 @jcocaramos - Droidcon NYC 2019

Slide 72

Slide 72 text

@jcocaramos - Droidcon NYC 2019

Slide 73

Slide 73 text

Recap → Complex problems require simple solutions → Compliment business goals with engineering goals → If you don't test it, you don't ship it → Automate all the things! → Developer experience is key to success @jcocaramos - Droidcon NYC 2019

Slide 74

Slide 74 text

Questions, comments, feedback... ? → @jcocaramos & @ChicagoFlutter → Dart 2 In Action → jorgecoca.dev ( ) → https://speakerdeck.com/jorgecoca/flutter-at- scale @jcocaramos - Droidcon NYC 2019

Slide 75

Slide 75 text

@jcocaramos - Droidcon NYC 2019

Slide 76

Slide 76 text

Happy coding! !" @jcocaramos - Droidcon NYC 2019