@@ -61,6 +61,10 @@ When the generator is run, `dart_define` reads the values from
61
61
` ` ` sh
62
62
# add the tool to dependencies
63
63
flutter pub add --dev dart_define
64
+
65
+ # If you chooce this method, you'll have to add `pub run`
66
+ # in from of all of the `dart_define` commands.
67
+ # Eg. `pub run dart_define generate`
64
68
` ` `
65
69
66
70
2. Define your variables in ` pubspec.yaml`
@@ -76,6 +80,18 @@ When the generator is run, `dart_define` reads the values from
76
80
# Defaults to true
77
81
json: true
78
82
83
+ # OPTIONAL: The path to the json file to generate.
84
+ # Defaults to dart_define.json
85
+ json_path: dart_define.json
86
+
87
+ # OPTIONAL: The path to the dart file to generate.
88
+ # Defaults to lib/dart_define.gen.dart
89
+ dart_path: lib/dart_define.gen.dart
90
+
91
+ # OPTIONAL: The name of the generated class.
92
+ # Defaults to DartDefine
93
+ class_name: DartDefine
94
+
79
95
# REQUIRED: The variables to add to the generated config files
80
96
variables:
81
97
@@ -102,6 +118,19 @@ When the generator is run, `dart_define` reads the values from
102
118
- name: BOOL_VALUE
103
119
description: An example bool value
104
120
required: true
121
+
122
+ # OPTIONAL: The flavors to use within the app
123
+ flavors:
124
+
125
+ # REQUIRED: The name of the flavor
126
+ - name: production
127
+
128
+ # REQUIRED: The description of the flavor
129
+ description: The production flavor
130
+ - name: staging
131
+ description: The staging flavor
132
+ - name: development
133
+ description: The development flavor
105
134
` ` `
106
135
107
136
3. Generate boilerplate
@@ -111,13 +140,6 @@ When the generator is run, `dart_define` reads the values from
111
140
dart_define generate
112
141
` ` `
113
142
114
- OR
115
-
116
- ` ` ` sh
117
- # If you added the tool to dependencies
118
- dart run dart_define generate
119
- ` ` `
120
-
121
143
> * NOTE: You can override values and variables from pubspec.yaml
122
144
> config with CLI arguments*
123
145
>
@@ -222,4 +244,65 @@ When the generator is run, `dart_define` reads the values from
222
244
flutter build ios --dart-define-from-file=dart_define.json
223
245
` ` `
224
246
247
+ # # Setting up flavors 🍬
248
+
249
+ ` dart_define` allows you to create multiple configurations, for different
250
+ environments or white labels of your app. This means you can easily generate
251
+ config files for all of the environments or white labels and launch your app
252
+ with the configurations. To set these up
253
+
254
+ 1. Add flavors to ` dart_define` config in ` pubspec.yaml`
255
+
256
+ ` ` ` yaml
257
+ dart_define:
258
+ variables:
259
+ - name: APP_NAME
260
+ description: The apps name
261
+ default: My App
262
+ required: false
263
+ - name: APP_ID
264
+ description: The apps unique id
265
+ default: com.my.app
266
+ required: false
267
+ flavors:
268
+ - name: production
269
+ description: The production flavor
270
+ - name: staging
271
+ description: The staging flavor
272
+ - name: development
273
+ description: The development flavor
274
+ ` ` `
275
+
276
+ 2. Generate the config file for all of your flavors
277
+
278
+ ` ` ` sh
279
+ dart_define generate --json_path=config/production.json --FLAVOR=production
280
+ dart_define generate --json_path=config/staging.json --FLAVOR=staging
281
+ dart_define generate --json_path=config/development.json --FLAVOR=development
282
+ ` ` `
283
+
284
+ 3. Use the flavor in your app
285
+
286
+ ` ` ` dart
287
+ /// ` dart_define` generates this enum based on the flavors in your configurations
288
+ /// You can access the current flavor used to launch the application by calling
289
+ /// DartDefine.flavor
290
+ enum Flavor {
291
+ /// The production flavor
292
+ production,
293
+
294
+ /// The staging flavor
295
+ staging,
296
+
297
+ /// The development flavor
298
+ development,
299
+ }
300
+ ` ` `
301
+
302
+ 4. Run application from specific flavor
303
+
304
+ ` ` ` sh
305
+ flutter run --dart-define-from-file=config/development.json
306
+ ` ` `
307
+
225
308
[1]: https://itnext.io/flutter-3-7-and-a-new-way-of-defining-compile-time-variables-f63db8a4f6e2
0 commit comments