सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
Flutter Gen Runner एक कोड जनरेटर है जो आस्थापना, फ़ॉन्ट, रंग और अन्य संसाधनों से सुरक्षित प्रकार के डार्ट कोड को स्वचालित रूप से बनाता है। इससे स्ट्रिंग-आधारित रेफरेंस की आवश्यकता नहीं होती है, जिससे रनटाइम त्रुटियाँ कम होती हैं और विकास गति में सुधार होता है। निर्मित स्थिरांक और उपयोगिता सुनिश्चित करते हैं कि प्रकार सुरक्षित हो और कोड को बनाए रखने में आसानी होती है। टूल फ्लटर प्रोजेक्ट में बिना किसी दिक्कत के एकीकृत होता है और एंड्रॉइड, आईओएस, मैकओएस, विंडोज और लिनक्स सहित कई प्लेटफॉर्म का समर्थन करता है।
उपयोग
प्रकार सुरक्षित संपत्ति संदर्भ उत्पन्न करना
फ़ॉन्ट और रंग स्थिर बनाने की स्वचालन
स्ट्रिंग-आधारित एपीआई के उपयोग को कम करना
संकलन समय जाँच के साथ कोड विश्वसनीयता में सुधार
UI विकास कार्यप्रवाह को सुगम बनाना
मुख्य विशेषताएँ
प्रकार सुरक्षित संसाधन उत्पादन
संपत्तियों, फॉन्ट, रंग और अधिक का समर्थन करता है
संकलन-समय त्रुटि रोकथाम
Flutter के साथ निरंतर एकीकरण
क्रॉस-प्लेटफॉर्म संगतता
इनके लिए उपयुक्त
UI संसाधनों में प्रकार सुरक्षा की तलाश करने वाले विकासकर्मी
स्ट्रिंग संबंधी बग को कम करने की कोशिश कर रही टीमें
स्केलेबल संपत्ति प्रबंधन की आवश्यकता वाले प्रोजेक्ट
व्यापक डिज़ाइन प्रणाली वाले Flutter ऐप्स
सीआई/सीडी पाइपलाइन्स को स्वचालित कोड जनरेशन की आवश्यकता होती है
विचार योग्य बातें
pubspec.yaml में सेटअप की आवश्यकता है
बिल्ड रनर निष्पादन की आवश्यकता है
प्रारंभिक परियोजना सेटअप बढ़ सकता है
गतिशील संसाधन लोडिंग के लिए उपयुक्त नहीं
विन्यास में सही परिसंपत्ति पथ पर निर्भर
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
The Flutter code generator for your assets, fonts, colors, … — Get rid of all String-based APIs.
Widgetbuild(BuildContext context) {
returnImage.asset('assets/images/profile.jpeg');
}
// The following assertion was thrown resolving an image codec:// Unable to load asset: assets/images/profile.jpeg
FlutterGen also supports dart pub workspaces.
Run build_runner from the workspace root, and make sure each workspace member
that should be built has resolution: workspace in its pubspec.yaml.
FlutterGen will resolve each package from the active build target instead of the
process working directory, so package-local pubspec.yaml configuration and
output paths continue to work in workspace builds.
If generated source files were removed manually while .dart_tool/build is
still present, run dart run build_runner clean from the workspace root before
running build --workspace again. The current post-process builder flow can
re-materialize files reliably after a clean build, but a warm incremental build
may skip unchanged manifests.
For workspace builds, use Dart >=3.7.0 together with build_runner >=2.12.0.
Use --workspace to treat the config file as a workspace root pubspec and run
generation for each listed workspace member. Without --workspace, the command
generates for exactly one package.
When --workspace is enabled, the root pubspec.yaml is used only to discover
workspace members from its workspace: section. FlutterGen then switches to
each member package and loads that package's own pubspec.yaml and, if
present, its local build.yaml.
That means workspace-wide command execution behaves like this:
Root pubspec.yaml: selects which packages to visit.
Member pubspec.yaml: provides flutter and flutter_gen configuration.
Member build.yaml: overrides generator options for that member only.
The --build option is only available in single-package mode. In workspace
mode, put any overrides in each package's local build.yaml instead.
Configuration file
FlutterGen generates dart files based on the key flutter and flutter_gen of pubspec.yaml.
Default configuration can be found here.
When using build_runner, you can also configure generator options in the
target build.yaml. These builder options are applied on top of the
pubspec.yaml configuration for the current target.
# build.yaml# ...targets:$default:builders:flutter_gen_runner:# or flutter_genoptions:output:lib/build_gen/# Optional (default: lib/gen/)line_length:120# Optional
# pubspec.yamlflutter:assets:-assets/images/-assets/images/chip3/chip.jpg-assets/images/chip4/chip.jpg-path:assets/images/icons/paint.svg-path:assets/images/icons/transformed.svgtransformers:-package:vector_graphics_compiler-assets/images/icons/dart@test.svg-assets/json/fruits.json-assets/flare/Penguin.flr-assets/rive/vehicles.riv-pictures/ocean_view.jpg# Also include assets from deferred components# https://docs.flutter.dev/perf/deferred-componentsdeferred-components:-name:myDeferredComponentassets:-assets/images/another_image.jps-assets/videos/a_large_video.mp4
These configurations will generate assets.gen.dart under the lib/gen/ directory by default.
Flavored assets
Flutter supports
Conditionally bundling assets based on flavor.
Assets are only available with flavors if specified.
flutter_gen will generate the specified flavors for assets regardless the current flavor.
The flavors field accessible though .flavors, for example:
You can use parse_animation to generate more animation details.
It will automatically parse all animation information for GIF and WebP files,
including frames, duration, etc. As this option significantly increases generation time,
The option is disabled by default; enabling it will significantly increase the generation elapse.
flutter_gen:images:parse_animation:true# <- Add this line (default: false)# This option implies parse_metadata: true when parsing images.
For GIF and WebP animation, several new nullable field is added to the
generated class. For example:
If you are using SVG images with flutter_svg you can use the integration feature. This feature also supports using vector_graphics_compiler transformer and the produced code will use the AssetBytesLoader for such transformed assets.
Note: For lottie integration with .lottie and .tgs files, you must add a custom decoder via decoder parameter, see lottie's document for more information.
In other cases, the asset is generated as String class.
// If don't use the Integrations.final svg = SvgPicture.asset(Assets.images.icons.paint);
final json = await rootBundle.loadString(Assets.json.fruits);
FlutterGen also support generating other style of Assets class:
Just follow the doc Use a custom font to specify fonts, then FlutterGen will generate related dart files.
No other specific configuration is required. Ignore duplicated.
FlutterGen can generate a Color class based on the name attribute and the color hex value.
If the element has the attribute type, then a specially color will be generated.
[SEVERE] flutter_gen_runner:flutter_gen_runner on $package$:
Bad state: No element
[SEVERE] Failed after 16.0s
The you most likely have a customized build.yaml to configure the build runner. In that case, all you have to do is to add the pubspec.yaml as build source to your build.yaml
targets:$default:sources:include:-pubspec.yaml# add this line-...