Skip to content

Commit c94329a

Browse files
committed
add more examples of library components in custom code
1 parent 40fd05a commit c94329a

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed

docs/ff-concepts/adding-customization/common-examples.md

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,28 +346,93 @@ Here’s a list of other Firebase Auth variables that can be referenced in Custo
346346
- These variables make it easy to integrate Firebase Auth data into custom functionality, enhancing the user experience.
347347
348348
349-
### Get Library Values in Custom Code
349+
### Get Dev Environment Values in Custom Code
350+
351+
Similar to the `FFLibraryValues` class, if you are using **[Dev Environments](../../testing-deployment-publishing/development-environments/development-environments.md)** in your FlutterFlow project, a new class called `FFDevEnvironmentValues` will be created. This class can also be accessed from custom code if needed. It is generated based on the environment selected by the user at the time of code generation.
352+
353+
To access any Dev Environment values in custom code, simply use:
354+
355+
```js
356+
Future getWebhookId() async {
357+
// Add your function code here!
358+
return FFDevEnvironmentValues().webhookId;
359+
}
360+
```
361+
362+
### Access Library Components in Custom Code
363+
364+
When using a library dependency in your project, you can also access its components, such as Library App State, Library Values, and Library Widgets, in the user project's custom code. Here are a few examples:
365+
366+
#### Get Library Values
350367
351368
Similar to the `FFAppState` class, FlutterFlow generates an `FFLibraryValues` class, which is also an abstract class. This class provides access to the **[Library Values](../../resources/projects/libraries.md#library-values)** if the project is a Library.
352369
353-
To directly access the Library Values in custom code:
370+
To access Library Values directly in custom code:
354371
355372
```js
356373
Future getSchema(StateStruct? syncStatus) async {
357374
print(FFLibraryValues().schema);
358375
}
359376
```
360377
361-
### Get Dev Environment Values in Custom Code
378+
#### Get Library Custom Code
362379
363-
Similar to the `FFLibraryValues` class, if you are using **[Dev Environments](../../testing-deployment-publishing/development-environments/development-environments.md)** in your FlutterFlow project, a new class called `FFDevEnvironmentValues` will be created. This class can also be accessed from custom code if needed. It is generated based on the environment selected by the user at the time of code generation.
380+
When you add a library dependency to your FlutterFlow project, FlutterFlow automatically includes necessary imports, allowing you to utilize custom code resources from the library project in your user project's custom code files.
364381
365-
To access any Dev Environment values in custom code, simply use:
382+
For example, if you have a library with project ID `library_hybw3o`, FlutterFlow will add the following import to your project:
366383
367384
```js
368-
Future getWebhookId() async {
369-
// Add your function code here!
370-
return FFDevEnvironmentValues().webhookId;
385+
import 'package:library_hybw3o/flutter_flow/custom_functions.dart' as library_hybw3o_functions;
386+
```
387+
388+
389+
Now, let's use the library’s custom functions in the user project's custom function:
390+
391+
```js
392+
int getRandomIndex(List<int> indexList) {
393+
final item = library_hybw3o_functions.getRandomItem(); // Library's custom function
394+
// get Random Index
395+
final randomNumber = math.Random();
396+
return ...
397+
}
398+
```
399+
400+
#### Manually add Library Imports
401+
If you do not see a library import in your project, you can manually import it and assign a custom alias.
402+
403+
For example, let's import the library's custom actions into the user project's Custom Widget resource.
404+
405+
If the import is not already available, you can add it manually as follows:
406+
407+
```js
408+
// Custom import
409+
import 'package:library_hybw3o/custom_code/actions/index.dart' as library_hybw3o_actions; // Assigning a custom alias to the import
410+
411+
// Example Widget code
412+
class CustomDialog extends StatefulWidget {
413+
const CustomDialog({
414+
super.key,
415+
this.width,
416+
this.height,
417+
});
418+
419+
final double? width;
420+
final double? height;
421+
422+
@override
423+
State<CustomDialog> createState() => _CustomDialogState();
424+
}
425+
426+
class _CustomDialogState extends State<CustomDialog> {
427+
@override
428+
void initState() {
429+
library_hybw3o_actions.getSchema(StateStruct()); // calling library custom action
430+
super.initState();
431+
}
432+
@override
433+
Widget build(BuildContext context) {
434+
return Container(height: 50, width: 50);
435+
}
371436
}
372437
```
373438

docs/resources/projects/libraries.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ It's important to note that these resources show up where they are instantiated.
202202

203203
This ensures that only relevant resources are shown where they are needed, optimizing performance and discoverability.
204204

205+
:::tip[Access Library Components in Custom Code]
206+
When your project includes a library dependency, you can use its components—such as Library App State, Library Values, Library Custom Code resources, etc.—in your custom code. Explore the **[Common Custom Code Examples](../../ff-concepts/adding-customization/common-examples.md#access-library-components-in-custom-code)** directory for reference.
207+
:::
208+
205209
![access-library-resources.avif](imgs/access-library-resources.avif)
206210

207211
## Library Versioning

0 commit comments

Comments
 (0)