-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
main.dart
112 lines (99 loc) · 3.29 KB
/
main.dart
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
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:get_it_example/app_model.dart';
// This is our global ServiceLocator
GetIt getIt = GetIt.instance;
void main() {
/// I use signalReady here only to show how to use it. In 99% of the cases
/// you don't need it. Just use registerSingletonAsync
getIt.registerSingleton<AppModel>(AppModelImplementation(),
signalsReady: true);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
// ignore: library_private_types_in_public_api
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
// Access the instance of the registered AppModel
// As we don't know for sure if AppModel is already ready we use isReady
getIt
.isReady<AppModel>()
.then((_) => getIt<AppModel>().addListener(update));
// Alternative
// getIt.getAsync<AppModel>().addListener(update);
super.initState();
}
@override
void dispose() {
getIt<AppModel>().removeListener(update);
super.dispose();
}
void update() => setState(() => {});
@override
Widget build(BuildContext context) {
return Material(
child: FutureBuilder(
future: getIt.allReady(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
getIt<AppModel>().counter.toString(),
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: getIt<AppModel>().incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
} else {
return const Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text('Waiting for initialisation'),
SizedBox(
height: 16,
),
CircularProgressIndicator(),
],
);
}
}),
);
}
}