-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCODE_FLOW.html
2217 lines (2063 loc) · 137 KB
/
CODE_FLOW.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Table of contents.</title>
<style>
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-yFRtMMDnQtDRO8rLpMIKrtPCD5jdktao2TV19YiZYWMDkUR5GQZR/NOVTdquEx1j" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/markdown.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/highlight.css">
<link href="https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.css" rel="stylesheet" type="text/css">
<style>
.task-list-item { list-style-type: none; } .task-list-item-checkbox { margin-left: -20px; vertical-align: middle; }
</style>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', 'Ubuntu', 'Droid Sans', sans-serif;
font-size: 14px;
line-height: 1.6;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.js"></script>
</head>
<body class="vscode-light">
<h2 id="table-of-contents">Table of contents.</h2>
<ol>
<li><a href="#table-of-contents">Table of contents.</a></li>
<li><a href="#what-is-this">What is this.</a></li>
<li><a href="#introduction">Introduction.</a></li>
<li><a href="#prerequisites">Prerequisites.</a></li>
<li><a href="#create-the-initial-flutter-app">Create the initial <code>Flutter</code> app.</a></li>
<li><a href="#folder-structure">Folder structure.</a>
<ol>
<li><a href="#explanations">Explanations</a></li>
</ol>
</li>
<li><a href="#reorganize-the-initial-app-and-navigate-with-named-routes">Reorganize the initial app and navigate with named routes.</a>
<ol>
<li><a href="#create-home-component">Create <code>Home</code> component.</a></li>
<li><a href="#create-myapp-component">Create <code>MyApp</code> component.</a></li>
<li><a href="#modify-mainapp-file">Modify <code>main.app</code> file.</a></li>
<li><a href="#navigate-between-components-using-named-route">Navigate between components using named route.</a></li>
<li><a href="#code-improvements">Code improvements</a></li>
<li><a href="#review">Review.</a></li>
</ol>
</li>
<li><a href="#bottom-navigation-bar-tab-bar">Bottom Navigation Bar (Tab Bar)</a>
<ol>
<li><a href="#explanations-1">Explanations.</a></li>
<li><a href="#code-improvements-1">Code improvements.</a></li>
<li><a href="#review-1">Review.</a></li>
</ol>
</li>
<li><a href="#create-pre-populated-database-and-use-dependency-injection-di--repository-patterns">Create pre-populated database and use <code>Dependency Injection (DI)</code> & <code>Repository</code> patterns.</a>
<ol>
<li><a href="#init-sqlite-database">Init Sqlite database.</a></li>
<li><a href="#dependency-injection-di-and-repository-patterns">Dependency Injection (DI) and Repository patterns.</a></li>
<li><a href="#review-2">Review.</a></li>
</ol>
</li>
<li><a href="#implement-observable-pattern-using-rxdart">Implement <code>Observable</code> pattern using <code>RxDart</code>.</a>
<ol>
<li><a href="#show-list-of-to-do-items-in-console-log">Show list of to-do items in console log.</a></li>
<li><a href="#show-list-on-home-page">Show list on <code>Home</code> page.</a></li>
<li><a href="#code-improvements-use-listviewbuilder-for-a-long-list">Code improvements: Use <code>ListView.builder</code> for a long list.</a></li>
<li><a href="#review-3">Review.</a></li>
</ol>
</li>
<li><a href="#implement-create-operation">Implement <code>Create</code> operation.</a>
<ol>
<li><a href="#save-new-item-to-database">Save new item to database.</a></li>
<li><a href="#code-improvements-2">Code improvements.</a></li>
<li><a href="#review-4">Review.</a></li>
</ol>
</li>
<li><a href="#implement-read-update-and-delete-operations">Implement <code>Read</code>, <code>Update</code> and <code>Delete</code> operations.</a>
<ol>
<li><a href="#implement-read-and-update-operations">Implement <code>Read</code> and <code>Update</code> operations.</a></li>
<li><a href="#implement-delete-operation">Implement <code>Delete</code> operation.</a></li>
<li><a href="#review-5">Review.</a></li>
</ol>
</li>
<li><a href="#use-global-variables-and-switching-theme-between-light-dark-mode">Use global variables and switching theme between light/ dark mode.</a>
<ol>
<li><a href="#review-6">Review.</a></li>
</ol>
</li>
<li><a href="#conclusion">Conclusion.</a></li>
<li><a href="#references">References:</a></li>
</ol>
<h2 id="what-is-this">What is this.</h2>
<p>This is the step-by-step instructions on how to create this app from scratch. The final app will look like this:</p>
<p><img src="file:////Volumes/MacData/CevoWorkspace/demo-apps/to_do_mobile_app/docs/images/screenshots/iOS-demo.gif" alt="alt text" title="Title">
<img src="file:////Volumes/MacData/CevoWorkspace/demo-apps/to_do_mobile_app/docs/images/screenshots/Android-demo.gif" alt="alt text" title="Title"></p>
<p>The source code and instruction article can be found on its <a href="https://github.com/tuanbs/to-do-mobile-app.git">Github</a> and <a href="https://github.com/tuanbs/to-do-mobile-app/blob/master/CODE_FLOW.html">CODE_FLOW.md</a> file.</p>
<h2 id="introduction">Introduction.</h2>
<p>Welcome to my first article on how to write a cross-platform mobile app using <code>Flutter</code>. -> What is <code>Flutter</code>? -> Please check <a href="https://flutter.dev/docs">here</a> to learn about it. -> In my words, it's a framework used to create cross-platform app (iOS, Android and Website). It's similar to <code>React Native</code> or <code>Ionic</code> frameworks if you've heard of them before.</p>
<p>You would ask me the question why I choose <code>Flutter</code> instead of <code>React Native</code> or <code>Ionic</code>. Well, I used those 2 frameworks before and I don't like it. The main reason I choose it because I come from <code>.Net</code> and <code>Angular</code> backgrounds, and I see I can reuse my knowledge and skills of <code>.Net</code> and <code>Angular</code> into <code>Flutter</code>. For example, one of my hobby apps uses <code>.Net Core</code> for its back-end API, <code>Angular</code> for the website and <code>Flutter</code> for the mobile app. And I see I can reuse about 80% of the concepts from <code>.Net</code> and <code>Angular</code> into <code>Flutter</code> app. So I would like to share it in this article.</p>
<p>What you'll learn in this post:</p>
<ul>
<li>How to build a <code>To-do</code> app that runs on <code>iOS</code> and <code>Android</code>.</li>
<li>Reuse some concepts from <code>.Net</code> and <code>Angular</code> into <code>Flutter</code> app.</li>
</ul>
<blockquote>
<p><strong>NOTE:</strong> In this article, I'm going to create the app that will run based on the local database named <code>Sqlite</code>, so that it doesn't need to be connected to internet. The roadmap (if I have time) for this project is to make it similar to the Apple <code>Notes</code> app on <code>iOS</code> devices, so that when it's connected to internet, it'll sync its local data with the server, and users can see their notes from different platforms (iOS, Android and website).</p>
</blockquote>
<h2 id="prerequisites">Prerequisites.</h2>
<p>This tutorial is for everyone who has experience with at least one of the programming languages such as <code>C#, Java, Javascript etc...</code>. However it would be easier for you to follow along with the code if you have the followings:</p>
<ul>
<li>Basic understanding of <code>Flutter</code> and <code>Dart</code>.</li>
<li>Familiar with some front-end frameworks like <code>Angular</code> or <code>React</code>.</li>
</ul>
<p>To code along with the code-flow in this post, please prepare the followings:</p>
<ul>
<li>Install the latest version of <code>Flutter</code> properly on your system. If you don't know that, check <a href="https://flutter.dev/docs">here</a> for more information.</li>
<li>Install Visual Studio Code (VSCode). You can use your preferred editor such as <code>Atom</code> or <code>Android Studio</code>.</li>
</ul>
<blockquote>
<p><strong>NOTE:</strong> At the time of writing this article, I use <code>Flutter</code> version <code>1.17.0</code> on macOS Catalina.</p>
</blockquote>
<p>Enough talking, let's create the app.</p>
<h2 id="create-the-initial-flutter-app">Create the initial <code>Flutter</code> app.</h2>
<p>To create the initial app, run cmd:</p>
<pre><code class="language-bash"><div>flutter create my_notes_flutter_mobile
</div></code></pre>
<p>Open the app in <code>VSCode</code> and press <code>Ctrl + F5</code>, then make sure you see the initial screen of flutter app without any error before moving to next step.</p>
<h2 id="folder-structure">Folder structure.</h2>
<p>I make this app's folder structure similar to the <code>Angular</code> app version I created before:</p>
<pre><code><code><div>my_notes_flutter_mobile
assets/
database/
to_do_mobile.db
images/
...
docs/
database_design/
...
images/
...
lib/
components/
app_tab_bar/
app_tab_bar.dart
add_to_do/
add_to_do.dart
edit_to_do/
edit_to_do.dart
home/
home.dart
settings/
settings.dart
shared/
components/
...
data/
repositories/
setting_repo_service.dart
to_do_repo_service.dart
models/
to_do_model.dart
parameters/
to_do_parameters.dart
to_do_resource_parameters.dart
...
services/
app_db_context_service.dart
app_constants.dart
app_globals.dart
app_injections.dart
app_routing.dart
main.dart
my_app.dart
test/
...
README.md
CODE_FLOW.md
pubspec.yaml
...
</div></code></code></pre>
<h3 id="explanations">Explanations</h3>
<p>In <code>Angular</code> or <code>React</code> app, we use the concept of <code>component</code> to implement the separation of concerns in software development. We can use this <code>components</code> approach in <code>Flutter</code> app as well.</p>
<blockquote>
<p><strong>NOTE:</strong> I, however, encourage people to use whatever the folder structure they prefer as long as it makes sense to them. And try to use the similar structure into different frameworks regardless of what programming language those frameworks are using. This approach enables you as a software developer to maintain the apps easier without worrying learning different concepts from different frameworks (or programming languages).</p>
</blockquote>
<h2 id="reorganize-the-initial-app-and-navigate-with-named-routes">Reorganize the initial app and navigate with named routes.</h2>
<p>In this step, we're going to reorganize the initial app using our folder structure mentioned above.</p>
<h3 id="create-home-component">Create <code>Home</code> component.</h3>
<p>Open <code>main.dart</code> file and refactor <code>MyHonePage</code> into <code>Home</code>.</p>
<blockquote>
<p><strong>Tips:</strong> To refactor class's name (or variables and methods) in <code>VSCode</code>, right-click on the class's name and select <code>Rename Symbol</code>. Then input the new name for it.</p>
</blockquote>
<p>Move this <code>Home</code> component into <code>lib/components/home.dart</code> file. By default, <code>Flutter</code> generates a new stateful component with the <code>title</code> argument inside its constructor. However, as we're not going to set component's title via its constructor, I prefer the title sticks inside its own component. So let's remove the <code>title</code> argument from its constructor like so:</p>
<blockquote>
<p><code>lib/components/home.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/cupertino.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatefulWidget</span> </span>{
<span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> <span class="hljs-built_in">String</span> title = <span class="hljs-string">'Home'</span>;
<span class="hljs-meta">@override</span>
_HomeState createState() => _HomeState();
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">_HomeState</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">State</span><<span class="hljs-title">Home</span>> </span>{
<span class="hljs-meta">@override</span>
Widget build(BuildContext context) {
<span class="hljs-keyword">return</span> Scaffold(
appBar: AppBar(
title: Text(Home.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
<span class="hljs-string">'This is Home page.'</span>,
),
],
),
),
);
}
}
</div></code></pre>
<h3 id="create-myapp-component">Create <code>MyApp</code> component.</h3>
<p>Open <code>main.dart</code> and move <code>MyApp</code> class into <code>my_app.dart</code> file. Then import the <code>Home</code> component.</p>
<blockquote>
<p><strong>NOTE:</strong> To avoid potential issue, you should import with the prefix <code>package:<path_to_component></code>. For example: <code>import 'package:to_do_mobile_app/components/home/home.dart';</code></p>
</blockquote>
<h3 id="modify-mainapp-file">Modify <code>main.app</code> file.</h3>
<p>Open <code>main.app</code> file and mark its <code>main()</code> function as <code>async</code> because we will need to have some configurations set up later on before running the app:</p>
<blockquote>
<p><code>lib/main.app</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span> {
<span class="hljs-keyword">var</span> myApp = MyApp();
runApp(myApp);
}
</div></code></pre>
<p>Now we're able to run the app using our own new folder structure. Make sure there's no error before moving to next step.</p>
<h3 id="navigate-between-components-using-named-route">Navigate between components using named route.</h3>
<p>Similar to <code>Angular</code> and <code>React</code>, in <code>Flutter</code> we can show the component based on its route name. Here I'm using <code>onGenerateRoute</code> to define app's routes. Let's modify the <code>MyApp</code> component as following:</p>
<blockquote>
<p><code>lib/my_app.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{
<span class="hljs-meta">@override</span>
Widget build(BuildContext context) {
<span class="hljs-keyword">return</span> MaterialApp(
title: <span class="hljs-string">'Flutter Demo'</span>,
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: <span class="hljs-string">'/'</span>,
onGenerateRoute: (RouteSettings settings) {
<span class="hljs-keyword">switch</span> (settings.name) {
<span class="hljs-keyword">case</span> <span class="hljs-string">'/'</span>:
<span class="hljs-keyword">return</span> CupertinoPageRoute(
builder: (_) => Home(),
);
<span class="hljs-keyword">default</span>:
<span class="hljs-keyword">assert</span>(<span class="hljs-keyword">false</span>, <span class="hljs-string">'No route defined for <span class="hljs-subst">${settings.name}</span>'</span>);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
}
},
<span class="hljs-comment">// home: Home(title: 'To Do Demo'),</span>
);
}
}
</div></code></pre>
<p>As the app gets scaled, we would have lots of routes and business logic in each route. So it's a best practice to separate the routes section into another class named <code>AppRouting</code>. This approach can also be used in <code>Angular</code> and <code>React</code>. Let's open <code>app_routing.dart</code> file and define the following routes:</p>
<blockquote>
<p><code>lib/app_routing.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/cupertino.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/components/home/home.dart'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppRouting</span> </span>{
<span class="hljs-keyword">static</span> Route<<span class="hljs-keyword">dynamic</span>> generateAppRoute(RouteSettings settings) {
<span class="hljs-keyword">switch</span> (settings.name) {
<span class="hljs-keyword">case</span> <span class="hljs-string">'/'</span>:
<span class="hljs-keyword">return</span> CupertinoPageRoute(
builder: (_) => Home(),
);
<span class="hljs-keyword">default</span>:
<span class="hljs-keyword">assert</span>(<span class="hljs-keyword">false</span>, <span class="hljs-string">'No route defined for <span class="hljs-subst">${settings.name}</span>'</span>);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
}
}
}
</div></code></pre>
<p>Then use this setting inside <code>MyApp</code> component as follow:</p>
<blockquote>
<p><code>lib/my_app.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/cupertino.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/app_routing.dart'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{
<span class="hljs-meta">@override</span>
Widget build(BuildContext context) {
<span class="hljs-keyword">return</span> MaterialApp(
title: <span class="hljs-string">'Flutter Demo'</span>,
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: <span class="hljs-string">'/'</span>,
onGenerateRoute: AppRouting.generateAppRoute,
);
}
}
</div></code></pre>
<h3 id="code-improvements">Code improvements</h3>
<p>As you can see we use the string <code>/</code> to name the <code>Home</code> component's route in <code>MyApp</code> class. And then inside <code>AppRouting</code> class, we repeat the <code>/</code> again to show the <code>Home</code> component. It's better to put those constant values into one place, so that when we update it, it would reflect the change to all of its references. This approach also reduces the duplicated code amd makes the app more maintainable. Let's create a class named <code>AppConstants</code>:</p>
<blockquote>
<p><code>/lib/app_constants.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/widgets.dart'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppConstants</span> </span>{
<span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> <span class="hljs-built_in">String</span> homePath = <span class="hljs-string">'/'</span>;
}
</div></code></pre>
<p>Then update the <code>MyApp</code> and <code>AppRouting</code> as follow:</p>
<blockquote>
<p><code>/lib/my_app.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-comment">// ...</span>
initialRoute: AppConstants.homePath,
<span class="hljs-comment">// ...</span>
</div></code></pre>
<blockquote>
<p><code>/lib/app_routing.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">switch</span> (settings.name) {
<span class="hljs-keyword">case</span> AppConstants.homePath:
<span class="hljs-keyword">return</span> CupertinoPageRoute(
builder: (_) => Home(),
);
<span class="hljs-keyword">default</span>:
<span class="hljs-keyword">assert</span>(<span class="hljs-keyword">false</span>, <span class="hljs-string">'No route defined for <span class="hljs-subst">${settings.name}</span>'</span>);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
}
</div></code></pre>
<h3 id="review">Review.</h3>
<p>What we have done so far:</p>
<ul>
<li>Structured the app's files and folders that is similar to <code>Angular</code> or <code>React</code>.</li>
<li>Navigating the app using named routes.</li>
<li>Put duplicated code and constants values inside <code>AppConstants</code> class.</li>
</ul>
<h2 id="bottom-navigation-bar-tab-bar">Bottom Navigation Bar (Tab Bar)</h2>
<p>In this section, we're going to implement bottom navigation bar (tab bar) using <code>Cupertino (iOS) style</code>.</p>
<p>You would ask the question why not <code>Android</code>, but <code>iOS style</code>? Well, <code>Android</code> uses <code>Hierarchical Navigation</code> which users have to trace back to the root of the app in order to go to another tab. Imagine you're at the 5th view of the 1st tab, and in order to get to the 2nd tab, you have to press the <code>Back</code> button 5 times to get back to the root and select the 2nd tab. So I think that's really annoying. -> That's why I prefer to use the <code>iOS style</code> bottom navigation which is called <code>Flat Navigation</code>. In flat navigation, users can jump between tabs without going back to the root of the app.</p>
<p>Let's create 2 components named <code>Settings</code> and <code>AppTabBar</code>. The <code>AppTabBar</code> has 2 tabs <code>Home</code> and <code>Settings</code> which will show <code>Home</code> and <code>Settings</code> component respectively when it's tapped:</p>
<blockquote>
<p><code>lib/settings/settings.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/cupertino.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Settings</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatefulWidget</span> </span>{
Settings({Key key}) : <span class="hljs-keyword">super</span>(key: key);
<span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> <span class="hljs-built_in">String</span> title = <span class="hljs-string">'Settings'</span>;
<span class="hljs-meta">@override</span>
_SettingsState createState() => _SettingsState();
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">_SettingsState</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">State</span><<span class="hljs-title">Settings</span>> </span>{
<span class="hljs-meta">@override</span>
Widget build(BuildContext context) {
<span class="hljs-keyword">return</span> Scaffold(
appBar: AppBar(
title: Text(Settings.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
<span class="hljs-string">'This is Settings page.'</span>,
),
],
),
),
);
}
}
</div></code></pre>
<blockquote>
<p><code>lib/app_tab_bar/app_tab_bar.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/cupertino.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/components/home/home.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/components/settings/settings.dart'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppTabBar</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatefulWidget</span> </span>{
<span class="hljs-meta">@override</span>
_AppTabBarState createState() => _AppTabBarState();
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">_AppTabBarState</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">State</span><<span class="hljs-title">AppTabBar</span>> </span>{
<span class="hljs-comment">//#region Lifecycle.</span>
<span class="hljs-meta">@override</span>
Widget build(BuildContext context) {
<span class="hljs-keyword">return</span> _buildCupertinoTabScaffold();
}
<span class="hljs-meta">@override</span>
<span class="hljs-keyword">void</span> dispose() {
<span class="hljs-keyword">super</span>.dispose();
}
<span class="hljs-comment">//#end-region.</span>
<span class="hljs-comment">//#region Helpers.</span>
CupertinoTabScaffold _buildCupertinoTabScaffold() {
<span class="hljs-keyword">return</span> CupertinoTabScaffold(
tabBar: CupertinoTabBar(
items: <BottomNavigationBarItem>[
_buildTabBarItem(CupertinoIcons.home, Home.title),
_buildTabBarItem(CupertinoIcons.settings, Settings.title),
],
),
tabBuilder: (context, index) {
<span class="hljs-keyword">return</span> CupertinoTabView(
builder: (context) {
<span class="hljs-keyword">switch</span> (index) {
<span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:
<span class="hljs-keyword">return</span> Home();
<span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:
<span class="hljs-keyword">return</span> Settings();
<span class="hljs-keyword">default</span>:
<span class="hljs-keyword">assert</span>(<span class="hljs-keyword">false</span>, <span class="hljs-string">'Unexpected tab'</span>);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
}
},
);
},
);
}
BottomNavigationBarItem _buildTabBarItem(IconData iconData, <span class="hljs-built_in">String</span> title) {
<span class="hljs-keyword">return</span> BottomNavigationBarItem(
icon: Icon(iconData),
title: Text(title),
);
}
<span class="hljs-comment">//#end-region.</span>
}
</div></code></pre>
<h3 id="explanations-1">Explanations.</h3>
<ul>
<li><code>tabBar</code>: <code>CupertinoTabBar</code> requires at least two items, or you will see errors at run-time. Those tab items are shown at the bottom of the app.</li>
<li><code>tabBuilder</code>: is responsible for making sure the specified tab is built. In this case, it calls a class constructor to set up each respective tab, wrapping all two in <code>CupertinoTabView</code> and <code>CupertinoPageScaffold</code>.</li>
</ul>
<p>Now, let's edit the following classes:</p>
<ul>
<li>In <code>AppConstant</code>, refactor the <code>homePath</code> into <code>appTabBarPath</code>:</li>
<li>In <code>AppRouting</code>, replace the <code>Home</code> component with the <code>AppTabBar</code> component.</li>
</ul>
<blockquote>
<p><code>lib/app_constants.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppConstants</span> </span>{
<span class="hljs-comment">// ...</span>
<span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> <span class="hljs-built_in">String</span> appTabBarPath = <span class="hljs-string">'/'</span>;
}
</div></code></pre>
<blockquote>
<p><code>lib/app_routing.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppRouting</span> </span>{
<span class="hljs-keyword">static</span> Route<<span class="hljs-keyword">dynamic</span>> generateAppRoute(RouteSettings settings) {
<span class="hljs-keyword">switch</span> (settings.name) {
<span class="hljs-keyword">case</span> AppConstants.appTabBarPath:
<span class="hljs-keyword">return</span> CupertinoPageRoute(
builder: (_) => AppTabBar(),
);
<span class="hljs-keyword">default</span>:
<span class="hljs-keyword">assert</span>(<span class="hljs-keyword">false</span>, <span class="hljs-string">'No route defined for <span class="hljs-subst">${settings.name}</span>'</span>);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
}
}
}
</div></code></pre>
<p>Run the app and you should see the following screen:</p>
<p><img src="file:////Volumes/MacData/CevoWorkspace/demo-apps/to_do_mobile_app/docs/images/screenshots/app-tab-bar.png" alt="alt text" title="Title"></p>
<p>Try to tap the <code>Settings</code> tab to see the <code>Settings</code> page.</p>
<h3 id="code-improvements-1">Code improvements.</h3>
<p>Let's improve the code a little bit. Instead of using <code>index</code> number of tab item, we can make it human-readable by using <code>enum</code>. Add this line at the beginning of your class:</p>
<blockquote>
<p><code>lib/app_tab_bar/app_tab_bar.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">enum</span> _TabItemLabelEnum { home, settings }
</div></code></pre>
<p>Then edit <code>_buildCupertinoTabScaffold</code> method as follow:</p>
<pre><code class="language-dart"><div>CupertinoTabScaffold _buildCupertinoTabScaffold() {
<span class="hljs-keyword">return</span> CupertinoTabScaffold(
tabBar: CupertinoTabBar(
items: <BottomNavigationBarItem>[
_buildTabBarItem(CupertinoIcons.home, Home.title),
_buildTabBarItem(CupertinoIcons.settings, Settings.title),
],
),
tabBuilder: (context, index) {
<span class="hljs-keyword">final</span> tabItemLabel = _TabItemLabelEnum.values[index];
<span class="hljs-keyword">return</span> CupertinoTabView(
builder: (context) {
<span class="hljs-keyword">switch</span> (tabItemLabel) {
<span class="hljs-keyword">case</span> _TabItemLabelEnum.home:
<span class="hljs-keyword">return</span> Home();
<span class="hljs-keyword">case</span> _TabItemLabelEnum.settings:
<span class="hljs-keyword">return</span> Settings();
<span class="hljs-keyword">default</span>:
<span class="hljs-keyword">assert</span>(<span class="hljs-keyword">false</span>, <span class="hljs-string">'Unexpected tab'</span>);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
}
},
);
},
);
}
</div></code></pre>
<h3 id="review-1">Review.</h3>
<p>What we have done so far:</p>
<ul>
<li>Created another component named <code>Settings</code> with basic UI.</li>
<li>Implemented <code>Bottom Navigation Bar</code> (tab bar) with iOS style and put it into a component named <code>AppTabBar</code>.</li>
<li>Using <code>enum</code> instead of <code>index</code> to make the code more human-readable.</li>
</ul>
<h2 id="create-pre-populated-database-and-use-dependency-injection-di--repository-patterns">Create pre-populated database and use <code>Dependency Injection (DI)</code> & <code>Repository</code> patterns.</h2>
<p>In this section, we're going to use <code>Sqlite</code> as local database so that the app can store its data locally. I already created the sql script that defines all tables for the app's database. This sql script is generated using <code>.Net EF Core</code> which will not be covered in this tutorial as this is the <code>Flutter</code> topic. Later, if have time, I'll write another tutorial on <code>.Net Core</code> and <code>EF Core</code> to create the back-end API for the app. Below is the sql script for Sqlite:</p>
<pre><code class="language-sql"><div><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-string">"AppUsers"</span> (
<span class="hljs-string">"Id"</span> <span class="hljs-built_in">INTEGER</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">CONSTRAINT</span> <span class="hljs-string">"PK_AppUsers"</span> PRIMARY <span class="hljs-keyword">KEY</span> AUTOINCREMENT,
<span class="hljs-string">"Guid"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"CreatedDate"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"UpdatedDate"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"IsDeleted"</span> <span class="hljs-built_in">INTEGER</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-number">0</span>,
<span class="hljs-string">"AccessFailedCount"</span> <span class="hljs-built_in">INTEGER</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"Email"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"Gender"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"PhoneNumber"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"UserName"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"DateOfBirth"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"FirstName"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"MiddleName"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"LastName"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-literal">NULL</span>
);
<span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-string">"ToDos"</span> (
<span class="hljs-string">"Id"</span> <span class="hljs-built_in">INTEGER</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">CONSTRAINT</span> <span class="hljs-string">"PK_ToDos"</span> PRIMARY <span class="hljs-keyword">KEY</span> AUTOINCREMENT,
<span class="hljs-string">"Guid"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"CreatedDate"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"UpdatedDate"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"IsDeleted"</span> <span class="hljs-built_in">INTEGER</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-number">0</span>,
<span class="hljs-string">"Description"</span> <span class="hljs-built_in">TEXT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-string">"IsDone"</span> <span class="hljs-built_in">INTEGER</span> <span class="hljs-literal">NULL</span> <span class="hljs-keyword">DEFAULT</span> <span class="hljs-number">0</span>,
<span class="hljs-string">"AppUserId"</span> <span class="hljs-built_in">INTEGER</span> <span class="hljs-literal">NULL</span>,
<span class="hljs-keyword">CONSTRAINT</span> <span class="hljs-string">"FK_ToDos_AppUsers_AppUserId"</span> <span class="hljs-keyword">FOREIGN</span> <span class="hljs-keyword">KEY</span> (<span class="hljs-string">"AppUserId"</span>) <span class="hljs-keyword">REFERENCES</span> <span class="hljs-string">"AppUsers"</span> (<span class="hljs-string">"Id"</span>) <span class="hljs-keyword">ON</span> <span class="hljs-keyword">DELETE</span> RESTRICT
);
<span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">UNIQUE</span> <span class="hljs-keyword">INDEX</span> <span class="hljs-string">"IX_AppUsers_Email"</span> <span class="hljs-keyword">ON</span> <span class="hljs-string">"AppUsers"</span> (<span class="hljs-string">"Email"</span>);
<span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">UNIQUE</span> <span class="hljs-keyword">INDEX</span> <span class="hljs-string">"IX_AppUsers_Guid"</span> <span class="hljs-keyword">ON</span> <span class="hljs-string">"AppUsers"</span> (<span class="hljs-string">"Guid"</span>);
<span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">UNIQUE</span> <span class="hljs-keyword">INDEX</span> <span class="hljs-string">"IX_AppUsers_UserName"</span> <span class="hljs-keyword">ON</span> <span class="hljs-string">"AppUsers"</span> (<span class="hljs-string">"UserName"</span>);
<span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">INDEX</span> <span class="hljs-string">"IX_ToDos_AppUserId"</span> <span class="hljs-keyword">ON</span> <span class="hljs-string">"ToDos"</span> (<span class="hljs-string">"AppUserId"</span>);
<span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">UNIQUE</span> <span class="hljs-keyword">INDEX</span> <span class="hljs-string">"IX_ToDos_Guid"</span> <span class="hljs-keyword">ON</span> <span class="hljs-string">"ToDos"</span> (<span class="hljs-string">"Guid"</span>);
<span class="hljs-comment">---------- Insert some sample data.</span>
<span class="hljs-keyword">Insert</span> <span class="hljs-keyword">Or</span> <span class="hljs-keyword">Replace</span> <span class="hljs-keyword">Into</span> ToDos(<span class="hljs-keyword">Id</span>, [Guid], CreatedDate, UpdatedDate, [Description], IsDone) <span class="hljs-keyword">Values</span> (<span class="hljs-number">1</span>, <span class="hljs-keyword">lower</span>(<span class="hljs-keyword">hex</span>(randomblob(<span class="hljs-number">16</span>))), datetime(<span class="hljs-string">'now'</span>), datetime(<span class="hljs-string">'now'</span>), <span class="hljs-string">"Learn Swift"</span>, <span class="hljs-number">0</span>);
<span class="hljs-keyword">Insert</span> <span class="hljs-keyword">Or</span> <span class="hljs-keyword">Replace</span> <span class="hljs-keyword">Into</span> ToDos(<span class="hljs-keyword">Id</span>, [Guid], CreatedDate, UpdatedDate, [Description], IsDone) <span class="hljs-keyword">Values</span> (<span class="hljs-number">2</span>, <span class="hljs-keyword">lower</span>(<span class="hljs-keyword">hex</span>(randomblob(<span class="hljs-number">16</span>))), datetime(<span class="hljs-string">'now'</span>), datetime(<span class="hljs-string">'now'</span>), <span class="hljs-string">"Learn Flutter"</span>, <span class="hljs-number">1</span>);
<span class="hljs-keyword">Insert</span> <span class="hljs-keyword">Or</span> <span class="hljs-keyword">Replace</span> <span class="hljs-keyword">Into</span> ToDos(<span class="hljs-keyword">Id</span>, [Guid], CreatedDate, UpdatedDate, [Description], IsDone) <span class="hljs-keyword">Values</span> (<span class="hljs-number">3</span>, <span class="hljs-keyword">lower</span>(<span class="hljs-keyword">hex</span>(randomblob(<span class="hljs-number">16</span>))), datetime(<span class="hljs-string">'now'</span>), datetime(<span class="hljs-string">'now'</span>), <span class="hljs-string">"Learn .Net Core"</span>, <span class="hljs-number">0</span>);
</div></code></pre>
<blockquote>
<p><strong>NOTE:</strong> In this tutorial, we're not going to use the <code>AppUsers</code> table. I leave it here for the next tutorial whose topic would be about authentication and sync the local database with the server database.</p>
</blockquote>
<p>Next, you need to download <a href="https://github.com/sqlitebrowser/sqlitebrowser">DB Browser for Sqlite</a> app to create database by executing the sql script. Then name your local db as <code>to_do_mobile.db</code> and put it in <code>assets/database/</code> folder. If you have issue generating database, you can use the one I attached in the source code on Github.</p>
<h3 id="init-sqlite-database">Init Sqlite database.</h3>
<p>After having the database, you need to tell <code>Flutter</code> that you want to use this database file inside the app as well as manipulating it to store the data. To achieve this, you need to specify your assets and install necessary packages to work with <code>sqlite</code> db in the <code>pubspec.yaml</code> file. Edit <code>pubspec.yaml</code> as below:</p>
<blockquote>
<p><code>pubspec.yaml</code></p>
</blockquote>
<pre><code class="language-yaml"><div><span class="hljs-comment"># ...</span>
<span class="hljs-attr">dependencies:</span>
<span class="hljs-attr"> flutter:</span>
<span class="hljs-attr"> sdk:</span> <span class="hljs-string">flutter</span>
<span class="hljs-attr"> sqflite:</span> <span class="hljs-number">1.3</span><span class="hljs-number">.0</span><span class="hljs-string">+1</span>
<span class="hljs-attr"> path_provider:</span> <span class="hljs-number">1.6</span><span class="hljs-number">.7</span>
<span class="hljs-attr"> path:</span> <span class="hljs-number">1.6</span><span class="hljs-number">.4</span>
<span class="hljs-attr"> intl:</span> <span class="hljs-number">0.16</span><span class="hljs-number">.1</span>
<span class="hljs-comment"># ...</span>
<span class="hljs-attr">flutter:</span>
<span class="hljs-comment"># ...</span>
<span class="hljs-attr"> assets:</span>
<span class="hljs-comment"># ...</span>
<span class="hljs-bullet"> -</span> <span class="hljs-string">assets/database/to_do_mobile.db</span>
<span class="hljs-comment"># ...</span>
</div></code></pre>
<p>As the names of database and its tables are constants, we should add them in <code>AppConstants</code> class:</p>
<blockquote>
<p><code>/lib/app_constants.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-comment">// ...</span>
<span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> <span class="hljs-built_in">String</span> databaseName = <span class="hljs-string">'to_do_mobile.db'</span>;
<span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> <span class="hljs-built_in">String</span> tableTodoName = <span class="hljs-string">'ToDos'</span>;
<span class="hljs-comment">// ...</span>
</div></code></pre>
<p>Next, similar to <code>.Net Core</code> concept, I'm going to create the service class named <code>AppDbContextService</code> in the <code>lib/shared/services</code> folder to manipulate the db:</p>
<blockquote>
<p><code>lib/shared/services/app_db_context_service.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">import</span> <span class="hljs-string">'dart:io'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'dart:typed_data'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/foundation.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/services.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:path/path.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:sqflite/sqflite.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/app_constants.dart'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppDbContextService</span> </span>{
<span class="hljs-built_in">String</span> _databasesPath;
Database _db;
AppDbContextService() {}
<span class="hljs-comment">//#region Helpers.</span>
<span class="hljs-comment">//#end-region.</span>
Future<<span class="hljs-keyword">void</span>> dropDb() <span class="hljs-keyword">async</span> {
<span class="hljs-keyword">try</span> {
_databasesPath = <span class="hljs-keyword">await</span> getDatabasesPath();
_databasesPath = join(_databasesPath, AppConstants.databaseName);
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">await</span> databaseExists(_databasesPath)) {
<span class="hljs-keyword">await</span> deleteDatabase(_databasesPath);
debugPrint(<span class="hljs-string">'Drop existing DB successfully'</span>);
}
} <span class="hljs-keyword">catch</span> (e) {
debugPrint(<span class="hljs-string">'Exception in AppDbContextService.dropDb(). e is: <span class="hljs-subst">$e</span>'</span>);
}
}
Future<<span class="hljs-keyword">void</span>> initDb() <span class="hljs-keyword">async</span> {
_databasesPath = <span class="hljs-keyword">await</span> getDatabasesPath();
_databasesPath = join(_databasesPath, AppConstants.databaseName);
<span class="hljs-comment">// Check if the database exists.</span>
<span class="hljs-built_in">bool</span> exists = <span class="hljs-keyword">await</span> databaseExists(_databasesPath);
<span class="hljs-keyword">if</span> (!exists) {
<span class="hljs-comment">// Should happen only the first time when you launch your app.</span>
debugPrint(<span class="hljs-string">"Database not exist. Creating new copy from assets."</span>);
<span class="hljs-comment">// Make sure the parent directory exists.</span>
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">await</span> Directory(dirname(_databasesPath)).create(recursive: <span class="hljs-keyword">true</span>);
<span class="hljs-comment">// Copy from asset.</span>
ByteData data = <span class="hljs-keyword">await</span> rootBundle.load(join(<span class="hljs-string">"assets/database"</span>, AppConstants.databaseName));
<span class="hljs-built_in">List</span><<span class="hljs-built_in">int</span>> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
<span class="hljs-comment">// Write and flush the bytes written.</span>
<span class="hljs-keyword">await</span> File(_databasesPath).writeAsBytes(bytes, flush: <span class="hljs-keyword">true</span>);
} <span class="hljs-keyword">catch</span> (e) {
debugPrint(<span class="hljs-string">'Exception in AppDbContextService.initDb(). e is: <span class="hljs-subst">$e</span>'</span>);
}
} <span class="hljs-keyword">else</span> {
debugPrint(<span class="hljs-string">"Database already exists."</span>);
}
<span class="hljs-comment">// open the database.</span>
_db = <span class="hljs-keyword">await</span> openDatabase(_databasesPath);
debugPrint(<span class="hljs-string">'initDb successfully. _databasesPath is: <span class="hljs-subst">$_databasesPath</span>'</span>);
}
}
</div></code></pre>
<blockquote>
<p><strong>NOTE:</strong> I also include the <code>dropDb</code> method in the above class, just in case you want to use it to refresh the database (call this method at <code>AppInjections</code> class).</p>
</blockquote>
<p>Next step is to implement the Dependency Injection (DI) pattern, which is used in <code>.Net Core</code> and <code>Angular</code> as well, to inject the <code>AppDbContext</code> service.</p>
<h3 id="dependency-injection-di-and-repository-patterns">Dependency Injection (DI) and Repository patterns.</h3>
<p>In software development, DI is a pattern in which an object receives other objects that it depends on. These other objects are called dependencies and in most cases they are instantiated once only. We call those dependencies as <code>services</code>. This pattern is used in many frameworks such as <code>Angular</code> and <code>.Net Core</code>.</p>
<p>In <code>.Net Core</code>, we also have another pattern called <code>Repositories</code> which are the classes or components that encapsulate the business logic required to access data sources. And we use DI to inject those repositories for the receiving objects. So we can call those repositories as <code>services</code> as well.</p>
<p>Alternatively, we can say DI is just the way we inject the objects when we need them, and those objects are created only once and shared for the entire app. And the so-called <code>Repositories</code> is just the way we separate the common codes (business logic codes) between controllers into objects which then can be shared between controllers.</p>
<p>I use those patterns quite a lot, so I'd like to implement them in <code>Flutter</code> app. I put the <code>Repositories</code> classes inside <code>lib/shared/data/repositories/</code> folder, and the <code>services</code> classes inside <code>lib/shared/services/</code> folder.</p>
<p>As <code>Flutter</code> doesn't support DI out of the box, we need to add the package <code>get_it</code> to use DI:</p>
<blockquote>
<p><code>pubspec.yaml</code></p>
</blockquote>
<pre><code class="language-yaml"><div><span class="hljs-comment"># ...</span>
<span class="hljs-attr"> get_it:</span> <span class="hljs-number">4.0</span><span class="hljs-number">.2</span>
<span class="hljs-comment"># ...</span>
</div></code></pre>
<blockquote>
<p><strong>NOTE:</strong> There's another package named <code>provider</code> which is also used to handle DI (not purely DI). But after playing around with it, I prefer <code>get_it</code> to <code>provider</code> because I see the syntax of <code>get_it</code> makes it easier to convert <code>Angular</code> app into <code>Flutter</code>.</p>
</blockquote>
<p>Then, we need to create <code>AppInjections</code> class to register the services we want to inject in this app:</p>
<blockquote>
<p><code>/lib/app_injections.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">import</span> <span class="hljs-string">'package:get_it/get_it.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/shared/services/app_db_context_service.dart'</span>;
<span class="hljs-keyword">final</span> GetIt getIt = GetIt.instance;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppInjections</span> </span>{
<span class="hljs-keyword">static</span> Future<<span class="hljs-keyword">void</span>> setupDI() <span class="hljs-keyword">async</span> {
<span class="hljs-keyword">final</span> appDbContextService = AppDbContextService();
<span class="hljs-comment">// Register services.</span>
getIt.registerSingletonAsync<AppDbContextService>(() <span class="hljs-keyword">async</span> {
<span class="hljs-keyword">return</span> appDbContextService;
});
<span class="hljs-comment">// Wait for db initialization.</span>
<span class="hljs-keyword">await</span> appDbContextService.initDb();
<span class="hljs-comment">// await appDbContextService.dropDb();</span>
}
}
</div></code></pre>
<p>Then call the method <code>setupDI</code> in <code>main.dart</code> to setup DI:</p>
<blockquote>
<p><code>lib/main.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">void</span> main() <span class="hljs-keyword">async</span> {
WidgetsFlutterBinding.ensureInitialized(); <span class="hljs-comment">// Ensure that the Flutter app initializes properly before initializing other configurations.</span>
<span class="hljs-keyword">var</span> myApp = MyApp();
<span class="hljs-keyword">await</span> AppInjections.setupDI();
runApp(myApp);
}
</div></code></pre>
<blockquote>
<p><strong>NOTE:</strong> <code>Dependency Injection</code> is a very common pattern that is used in many popular frameworks such as <code>Angular</code> and <code>.Net Core</code>. That's why I suggest you should use it in <code>Flutter</code> app.</p>
</blockquote>
<p>Now when you run your app (on iOS), you should see the log console like so:</p>
<pre><code class="language-bash"><div>flutter: Database not exist. Creating new copy from assets.
flutter: initDb successfully. _databasesPath is: <path_to_database>/Documents/to_do_mobile.db
</div></code></pre>
<p>When you run the app, each platform (<code>iOS</code> or <code>Android</code>) will have its own way to keep this db in a particular folder. You can use <code>DB Browser</code> app to open the db (<code>iOS</code> simulator in my case) at <code><path_to_database>/Documents/to_do_mobile.db</code> and check to see if it's the same as the pre-populated DB in the <code>assets/database/to_do_mobile.db</code>.</p>
<h3 id="review-2">Review.</h3>
<p>What we have done so far:</p>
<ul>
<li>Created the pre-populated database named <code>to_do_mobile.db</code>.</li>
<li>Installed necessary packages to support <code>Sqlite</code> in this app.</li>
<li>Created <code>AppDbContextService</code> to init the database.</li>
<li>Implemented Dependency Injections (DI) design pattern to inject the database service into the app.</li>
</ul>
<h2 id="implement-observable-pattern-using-rxdart">Implement <code>Observable</code> pattern using <code>RxDart</code>.</h2>
<p>In this section, I'm going to use <code>Observable</code> pattern which will be used extensively in the rest of this article. The main reason I use this pattern is because it's a cross-platform technique. Once we're familiar with it, we can reuse it in other programming languages such as <code>Javascript</code> (using <code>RxJS</code>), <code>Swift</code> (using <code>RxSwift</code>) or <code>Kotlin</code> (using <code>RxKotlin</code>). I like this pattern which is also used in my <code>Angular</code> apps and would like to use it in this <code>Flutter</code> app, so that I can bring the logic from <code>Angular</code> into <code>Flutter</code>. I won't go in depth into <code>Observable</code> but will just cover some of the high level concepts.</p>
<p>According to <a href="https://en.wikipedia.org/wiki/Observer_pattern">wikipedia</a>:</p>
<blockquote>
<p>The <code>observer</code> pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.</p>
</blockquote>
<p>This pattern follows the idea of unidirectional data flow which may seem familiar if you have worked with <code>Flux/ Redux</code> in <code>React</code>. It ensures that the data is coming from one place (single source of truth) in the application and that every component receives the latest version of that data through the data streams.</p>
<p>Alternatively, in my words, I would say <code>Observable</code> pattern is when all components listen to a particular data source and this data source must come from one place. Once this data source is changed, this change will be reflected to all components.</p>
<blockquote>
<p><strong>NOTE:</strong> The implementation of using <code>Observable</code> pattern in this article may seem like a lot of work for such a simple todo app, but when we scale this up to a very large app we'll see <code>Observable</code> can really help manage the data and application state.</p>
</blockquote>
<p>Enough talking, let's understand it by implementing it in this app.</p>
<h3 id="show-list-of-to-do-items-in-console-log">Show list of to-do items in console log.</h3>
<p>We're going to implement <code>repository</code> pattern which is mentioned in previous section to handle the CRUD operations, and install <code>rxdart</code> package to implement <code>Observable</code> pattern.</p>
<p>Add the <code>rxdart</code> package:</p>
<blockquote>
<p><code>pubspec.yaml</code></p>
</blockquote>
<pre><code class="language-yaml"><div><span class="hljs-attr">rxdart:</span> <span class="hljs-number">0.24</span><span class="hljs-number">.0</span>
</div></code></pre>
<p>Before showing the list of item on the UI, I'd like to see it in the console log first. We need to make the following changes:</p>
<ul>
<li>Create a <code>ToDo</code> model class to capture the to-do item.</li>
<li>Create <code>ToDoResourceParameters</code> class which will be used to pass the <code>searchQuery</code> value when implementing the search feature later.
<ul>
<li><strong>NOTE:</strong> I won't cover the search feature in this article due to the time limit I have. But you can check its implementation in the source code when I update it.</li>
</ul>
</li>
<li>Edit <code>AppDbContext</code> to add the data source of <code>toDos</code>.</li>
<li>Create <code>ToDoRepoService</code> in <code>lib/shared/data/repositories</code> and create an <code>Observable</code> stream of <code>toDos</code> to subscribe to.</li>
<li>Register <code>ToDoRepoService</code> in <code>AppInjections</code> class as part of <code>DI</code>.</li>
<li>Edit <code>Home</code> component to subscribe to the <code>toDos</code> data source stream then call <code>setState</code> to update the UI when data is changed. -> And show to-do list on console.</li>
</ul>
<blockquote>
<p><code>lib/shared/models/to_do_model.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ToDo</span> </span>{
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-built_in">String</span> idColumn = <span class="hljs-string">'Id'</span>;
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-built_in">String</span> createdDateColumn = <span class="hljs-string">'CreatedDate'</span>;
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-built_in">String</span> descriptionColumn = <span class="hljs-string">'Description'</span>;
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-built_in">String</span> guidColumn = <span class="hljs-string">'Guid'</span>;
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-built_in">String</span> isDeletedColumn = <span class="hljs-string">'IsDeleted'</span>;
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-built_in">String</span> isDoneColumn = <span class="hljs-string">'IsDone'</span>;
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-built_in">String</span> updatedDateColumn = <span class="hljs-string">'UpdatedDate'</span>;
<span class="hljs-built_in">int</span> id;
<span class="hljs-built_in">String</span> createdDate;
<span class="hljs-built_in">String</span> description;
<span class="hljs-built_in">String</span> guid;
<span class="hljs-built_in">bool</span> isDeleted;
<span class="hljs-built_in">bool</span> isDone;
<span class="hljs-built_in">String</span> updatedDate;
ToDo(
{<span class="hljs-keyword">this</span>.id,
<span class="hljs-keyword">this</span>.createdDate,
<span class="hljs-keyword">this</span>.description,
<span class="hljs-keyword">this</span>.guid,
<span class="hljs-keyword">this</span>.isDeleted,
<span class="hljs-keyword">this</span>.isDone,
<span class="hljs-keyword">this</span>.updatedDate});
ToDo.fromJson(<span class="hljs-built_in">Map</span><<span class="hljs-built_in">String</span>, <span class="hljs-keyword">dynamic</span>> json) {
id = json[idColumn];
createdDate = json[createdDateColumn];
description = json[descriptionColumn];
guid = json[guidColumn];
isDeleted = json[isDeletedColumn] == <span class="hljs-number">1</span>;
isDone = json[isDoneColumn] == <span class="hljs-number">1</span>;
updatedDate = json[updatedDateColumn];
}
<span class="hljs-built_in">Map</span><<span class="hljs-built_in">String</span>, <span class="hljs-keyword">dynamic</span>> toJson() {
<span class="hljs-keyword">final</span> <span class="hljs-built_in">Map</span><<span class="hljs-built_in">String</span>, <span class="hljs-keyword">dynamic</span>> data = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span><<span class="hljs-built_in">String</span>, <span class="hljs-keyword">dynamic</span>>();
<span class="hljs-keyword">if</span> (id != <span class="hljs-keyword">null</span>) {
data[idColumn] = id;
}
data[createdDateColumn] = createdDate;
data[descriptionColumn] = description;
data[guidColumn] = guid;
data[isDeletedColumn] = isDeleted == <span class="hljs-keyword">true</span> ? <span class="hljs-number">1</span> : <span class="hljs-number">0</span>;
data[isDoneColumn] = isDone == <span class="hljs-keyword">true</span> ? <span class="hljs-number">1</span> : <span class="hljs-number">0</span>;
data[updatedDateColumn] = updatedDate;
<span class="hljs-keyword">return</span> data;
}
}
</div></code></pre>
<blockquote>
<p><code>/lib/shared/parameters/to_do_resource_parameters.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ToDoResourceParameters</span> </span>{
<span class="hljs-keyword">final</span> <span class="hljs-built_in">String</span> searchQuery;
ToDoResourceParameters({<span class="hljs-keyword">this</span>.searchQuery});
<span class="hljs-built_in">Map</span><<span class="hljs-built_in">String</span>, <span class="hljs-keyword">dynamic</span>> toJson() {
<span class="hljs-keyword">final</span> <span class="hljs-built_in">Map</span><<span class="hljs-built_in">String</span>, <span class="hljs-keyword">dynamic</span>> data = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span><<span class="hljs-built_in">String</span>, <span class="hljs-keyword">dynamic</span>>();
data[<span class="hljs-string">'searchQuery'</span>] = <span class="hljs-keyword">this</span>.searchQuery;
<span class="hljs-keyword">return</span> data;
}
}
</div></code></pre>
<blockquote>
<p><code>lib/shared/services/app_db_context_service.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppDbContextService</span> </span>{
<span class="hljs-comment">// ...</span>
<span class="hljs-comment">/* DataStore stuffs for ToDos. */</span>
<span class="hljs-built_in">List</span><ToDo> _toDos;
<span class="hljs-built_in">List</span><ToDo> <span class="hljs-keyword">get</span> toDos { <span class="hljs-keyword">return</span> _toDos; }
<span class="hljs-keyword">set</span> toDos(<span class="hljs-built_in">List</span><ToDo> value) { _toDos = value ?? <span class="hljs-keyword">null</span>; }
AppDbContextService() {
<span class="hljs-comment">// RxDart stuffs.</span>
toDos = [];
}
<span class="hljs-comment">// ...</span>
}
</div></code></pre>
<blockquote>
<p><code>/lib/shared/services/repositories/to_do_repo_service.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/foundation.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:rxdart/rxdart.dart'</span> <span class="hljs-keyword">as</span> rxdart;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/app_constants.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/shared/models/to_do_model.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/shared/parameters/to_do_resource_parameters.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/shared/services/app_db_context_service.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/app_injections.dart'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ToDoRepoService</span> </span>{
<span class="hljs-keyword">var</span> _toDosBehaviorSubject = rxdart.BehaviorSubject<<span class="hljs-built_in">List</span><ToDo>>();
Stream<<span class="hljs-built_in">List</span><ToDo>> <span class="hljs-keyword">get</span> toDosObservable => _toDosBehaviorSubject.asBroadcastStream();
AppDbContextService _appDbContextService;
ToDoRepoService() {
_appDbContextService = _appDbContextService ?? getIt<AppDbContextService>();
}
Future getListAsync(ToDoResourceParameters toDoResourceParameters) <span class="hljs-keyword">async</span> {
<span class="hljs-keyword">dynamic</span> error;
<span class="hljs-keyword">try</span> {
<span class="hljs-comment">// Get data from local Sqlite Db. In the web app (like Angular or React), we get data by using `http`.</span>
<span class="hljs-keyword">var</span> readOnlyList = <span class="hljs-keyword">await</span> _appDbContextService.database?.query(
AppConstants.tableTodoName,
columns: [ToDo.idColumn, ToDo.descriptionColumn, ToDo.guidColumn, ToDo.isDoneColumn, ToDo.isDeletedColumn],
where: <span class="hljs-string">'<span class="hljs-subst">${ToDo.isDeletedColumn}</span> = ?'</span>, whereArgs: [<span class="hljs-number">0</span>],
);
<span class="hljs-built_in">List</span><ToDo> list = readOnlyList?.map((item) => ToDo.fromJson(item))?.toList();
_appDbContextService.toDos = <span class="hljs-built_in">List</span><ToDo>.from(list); <span class="hljs-comment">// Create a new map to modify it in memory.</span>
<span class="hljs-comment">// Notify to all listeners.</span>
_toDosBehaviorSubject.add(_appDbContextService.toDos);
<span class="hljs-keyword">return</span> Future.value();
} <span class="hljs-keyword">catch</span> (e) {
error = e;
debugPrint(<span class="hljs-string">'ToDoRepoService getListAsync failed. error is: <span class="hljs-subst">$error</span>'</span>);
}
<span class="hljs-keyword">return</span> Future.error(error);
}
}
</div></code></pre>
<blockquote>
<p><code>/lib/app_injections.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppInjections</span> </span>{
<span class="hljs-keyword">static</span> Future<<span class="hljs-keyword">void</span>> setupDI() <span class="hljs-keyword">async</span> {
<span class="hljs-comment">// Register services.</span>
...
getIt.registerSingletonAsync<ToDoRepoService>(() <span class="hljs-keyword">async</span> => ToDoRepoService());
}
}
</div></code></pre>
<blockquote>
<p><code>/lib/components/home/home.dart</code></p>
</blockquote>
<pre><code class="language-dart"><div><span class="hljs-keyword">import</span> <span class="hljs-string">'dart:async'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'dart:convert'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/cupertino.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/app_injections.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/shared/data/repositories/to_do_repo_service.dart'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'package:to_do_mobile_app/shared/models/to_do_model.dart'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatefulWidget</span> </span>{
<span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> <span class="hljs-built_in">String</span> title = <span class="hljs-string">'Home'</span>;
<span class="hljs-meta">@override</span>
_HomeState createState() => _HomeState();
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">_HomeState</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">State</span><<span class="hljs-title">Home</span>> </span>{
StreamSubscription<<span class="hljs-built_in">List</span><ToDo>> _toDosObservableSubscriber;
<span class="hljs-built_in">List</span><ToDo> _toDos;
<span class="hljs-comment">/* DI Services vars. */</span>
ToDoRepoService _toDoRepoService;
_HomeState() {
<span class="hljs-comment">/* DI Services. */</span>
_toDoRepoService = _toDoRepoService ?? getIt<ToDoRepoService>();
}
<span class="hljs-comment">//#region Lifecycle.</span>
<span class="hljs-meta">@override</span>
<span class="hljs-keyword">void</span> initState() {
<span class="hljs-keyword">super</span>.initState();
_toDosObservableSubscriber = _toDoRepoService.toDosObservable.listen((data) {
debugPrint(json.encode(data));
_toDos = data;
<span class="hljs-keyword">if</span> (_toDos != <span class="hljs-keyword">null</span>) {
setState(() {});
}
});
<span class="hljs-keyword">this</span>._getListAsync();
}
<span class="hljs-meta">@override</span>
Widget build(BuildContext context) {
...
}
<span class="hljs-meta">@override</span>
<span class="hljs-keyword">void</span> dispose() {
<span class="hljs-keyword">super</span>.dispose();
_toDosObservableSubscriber.cancel();
}
<span class="hljs-comment">//#end-region.</span>