I have this JavaScript class:
'use strict;'
/* global conf */
var properties = {
'PROPERTIES': {
'CHANNEL': 'sport',
'VIEW_ELEMENTS': {
'LOADER_CLASS': '.loader',
'SPLASH_CLASS': '.splash'
}
}
};
In JavaScript I can use these properties: properties.PROPERTIES.CHANNEL
Is it possible to convert this to DART? Is there a best practise to do that?
There are different way.
You could just create a map
my_config.dart
const Map properties = const {
'CHANNEL': 'sport',
'VIEW_ELEMENTS': const {
'LOADER_CLASS': '.loader',
'SPLASH_CLASS': '.splash'
}
}
then use it like
main.dart
import 'my_config.dart';
main() {
print(properties['VIEW_ELEMENTS']['SPLASH_CLASS']);
}
or you can use classes to get proper autocompletion and type checking
my_config.dart
const properties = const Properties('sport', const ViewElements('.loader', '.splash'));
class Properties {
final String channel;
final ViewElements viewElements;
const Properties(this.channel, this.viewElements;
}
class ViewElements {
final String loaderClass;
final String splashClass;
const ViewElements(this.loaderClass, this.splashClass);
}
main.dart
import 'my_config.dart';
main() {
print(properties.viewElements.splashClass);
}
Following up on the above answer using classes, it may be convenient to implement static variables, the downside is that it still must be compiled/rebuilt.
class CONFIG {
static final String BUILD = "Release";
static final String DEPLOYMENT = "None";
}
This can be used from a separate class after importing via:
var xyz = CONFIG.BUILD;
Related
Let's say we want to create a simple dart file, that just declares a class containing consts. E.g.
class StringNames {
static const String helloWorld = 'Hello World';
}
But rather than write this manually, we want to generate this file programmatically. We want a program that will create this file for us.
We could write such a program in dart itself, e.g.
import 'dart:io';
void main() {
final f1 = 'file1.dart';
File(f1).writeAsString('class stringNames {\nstatic const String helloWorld = \'HelloWorld\';\n}');
}
Naturally this feels wrong. Is there a better way to achieve this?
You could use multi-line string as described here in the Dart language tour:
https://dart.dev/guides/language/language-tour#strings
So your example becomes like this, which is much more readable:
import 'dart:io';
void main() {
final f1 = 'file1.dart';
File(f1).writeAsString('''
class StringNames {
static const String helloWorld = 'Hello World';
}
''');
}
You can also use this and insert values from variables inside the string like this:
import 'dart:io';
void main() {
final f1 = 'file1.dart';
final someValue = 42;
File(f1).writeAsString('''
class StringNames {
static const String helloWorld = 'Hello World';
static const int someValue = $someValue;
}
''');
}
I was working on iOS, and now I have to deal with flutter.
The case is when I was using swift, I'm able to access the rule variable with different class type based on different enum cases.
example code as below:
enum SensorTypeRule{
case Lamp(rule:LampRule)
case Counter(rule:CounterRule)
}
struct LampRule{
let ruleTriggerColor: LampColor
let ruleSustainedMilliseconds: UInt32
}
struct CounterRule{
let ruleLimit: UInt32
}
and can be access like below:
let sensorTypeRule:SensorTypeRule = someSensorTypeRuleInstance
switch sensorTypeRule{
case .Lamp(let rule):
print("\(rule. ruleSustainedMilliseconds)")
case .Counter(let rule):
print("\(rule.ruleLimit)")
}
Is there an equivalent approach in dart?
Dart does not have the concept of sealed classes, however you can do this way:
// Create an abstract class representing an enum
// This enum can be instantiated in two ways: either
// calling SensorTypeRule.lamp or SensorTypeRule.counter
abstract class SensorTypeRule {
const factory SensorTypeRule.lamp(LampRule rule) = Lamp._;
const factory SensorTypeRule.counter(CounterRule rule) = Counter._;
const SensorTypeRule();
}
// Create the rules inside each respective class
// Using _ as constructor name disallows the user
// to instantiate it directly -> Lamp(...)
// Instead, it must use the base class -> SensorTypeRule.lamp(...)
class Lamp extends SensorTypeRule {
final LampRule rule;
const Lamp._(this.rule);
}
class Counter extends SensorTypeRule {
final CounterRule rule;
const Counter._(this.rule);
}
// Define the rules
class LampRule {
final LampColor ruleTriggerColor;
final int ruleSustainedMilliseconds;
const LampRule({
required this.ruleTriggerColor,
required this.ruleSustainedMilliseconds,
});
}
class CounterRule {
final int ruleLimit;
const CounterRule({
required this.ruleLimit,
});
}
When accessing it, you can do this way:
final SensorTypeRule sensorTypeRule =
SensorTypeRule.counter(CounterRule(ruleLimit: 10));
if (sensorTypeRule is Lamp) {
print(sensorTypeRule.rule.ruleSustainedMilliseconds);
} else if (sensorTypeRule is Counter) {
print(sensorTypeRule.rule.ruleLimit);
}
I try to localize a String in Flutter with the localization package. The problem is the location where my translation is needed. It is not related to the UI, rather it is somewhere deep in my model, where I don't have access to a BuildContext. Is there any other possibility to still make use of the translation function?
// I don't have a context variable here
MyLocalizations.of(context).trans("foo")
Yes there is. You don't need BuildContext to access strings. Here is my solution:
class Strings {
Strings._(Locale locale) : _localeName = locale.toString() {
current = this;
}
final String _localeName;
static Strings current;
static Future<Strings> load(Locale locale) async {
await initializeMessages(locale.toString());
final result = Strings._(locale);
return result;
}
static Strings of(BuildContext context) {
return Localizations.of<Strings>(context, Strings);
}
String get title {
return Intl.message(
'Hello World',
name: 'title',
desc: 'Title for the Demo application',
);
}
}
Future<Null> main() async {
final Locale myLocale = Locale(window.locale);
await Strings.load(myLocale);
runApp(MyApplication());
}
Now you can reference a string as follows:
final title = Strings.current.title;
I know this question is dated way back. But I came across this issue when implementing my application, and I dont see any "nice" way to handle it.
So here is my approach
class LanguageService {
static String defaultLanguage = 'en';
static Map<String, Map<String, String>> _localizedValues = {
'en': {
'title': 'Storefront',
'language': 'Language',
'googleLogin': 'Login with Google'
},
'vn': {
'title': 'Cửa hàng',
'language': 'Ngôn ngữ',
'googleLogin': 'Đăng Nhập với Google'
}
};
static set language(String lang) {
defaultLanguage = lang;
}
static String get title {
return _localizedValues[defaultLanguage]['title'];
}
static String get language {
return _localizedValues[defaultLanguage]['language'];
}
static String get googleLogin {
return _localizedValues[defaultLanguage]['googleLogin'];
}
}
Now you can reference a string as follows:
String title = LanguageService.title;
You can find the detailed tutorial here
AppLocalitzations needs the context.
You can create a class (e.g., Localization) to encapsulate AppLocalizations initialization and initialize it from the home widget using its context. After, can be used with a mixin:
import 'package:flutter/widgets.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class Localization {
static AppLocalizations _loc;
AppLocalizations get loc => Localization._loc;
static void init(BuildContext context) => _loc = AppLocalizations.of(context);
}
In the home widget:
...
#override
Widget build(BuildContext context) {
Localization.init(context);
return Scaffold(
...
Access to loc in some class (it isn't necessary to be a Widget) using mixins:
class XXXWidget extends StatelessWidget with Localization {
...
Text(loc.xxxx)
...
}
class _XXXXWidgetState extends State<XXXWidget> with Localization {
...
Text(loc.xxxx)
...
}
class XXXXController with Localization {
...
cardNumberValidator = RequiredValidator(errorText: loc.commons_Required);
...
}
Null safety version:
class Localization {
static AppLocalizations? _l;
AppLocalizations get loc => Localization._l!;
static void init(BuildContext context) => _l = AppLocalizations.of(context)!;
}
No, there is no other way because it is stored using an InheritedWidget, which is a part of the build tree and thus can only be accessed with a reference to it (the BuildContext).
You will need to pass your context to somewhere deep in your model.
I am not sure if i did it right (from performance point of view) and maybe someone can comment on this but i have rx BehaviorSubject in my AppLocalization and fire event once new locales are loaded. I am listening to it in my main.dart and doing setState on receiving an event.
I checked performance tab but did not noticed any big changes in it once comparing my method vs accessing translations through context (inherited widget).
I have a list of models that I need to create a mini reflective system.
I analyzed the Serializable package and understood how to create one generated file per file, however, I couldn't find how can I create one file for a bulk of files.
So, how to dynamically generate one file, using source_gen, for a list of files?
Example:
Files
user.dart
category.dart
Generated:
info.dart (containg information from user.dart and category.dart)
Found out how to do it with the help of people in Gitter.
You must have one file, even if empty, to call the generator. In my example, it is lib/batch.dart.
source_gen: ^0.5.8
Here is the working code:
The tool/build.dart
import 'package:build_runner/build_runner.dart';
import 'package:raoni_global/phase.dart';
main() async {
PhaseGroup pg = new PhaseGroup()
..addPhase(batchModelablePhase(const ['lib/batch.dart']));
await build(pg,
deleteFilesByDefault: true);
}
The phase:
batchModelablePhase([Iterable<String> globs =
const ['bin/**.dart', 'web/**.dart', 'lib/**.dart']]) {
return new Phase()
..addAction(
new GeneratorBuilder(const
[const BatchGenerator()], isStandalone: true
),
new InputSet(new PackageGraph.forThisPackage().root.name, globs));
}
The generator:
import 'dart:async';
import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';
import 'package:glob/glob.dart';
import 'package:build_runner/build_runner.dart';
class BatchGenerator extends Generator {
final String path;
const BatchGenerator({this.path: 'lib/models/*.dart'});
#override
Future<String> generate(Element element, BuildStep buildStep) async {
// this makes sure we parse one time only
if (element is! LibraryElement)
return null;
String libraryName = 'raoni_global', filePath = 'lib/src/model.dart';
String className = 'Modelable';
// find the files at the path designed
var l = buildStep.findAssets(new Glob(path));
// get the type of annotation that we will use to search classes
var resolver = await buildStep.resolver;
var assetWithAnnotationClass = new AssetId(libraryName, filePath);
var annotationLibrary = resolver.getLibrary(assetWithAnnotationClass);
var exposed = annotationLibrary.getType(className).type;
// the caller library' name
String libName = new PackageGraph.forThisPackage().root.name;
await Future.forEach(l.toList(), (AssetId aid) async {
LibraryElement lib;
try {
lib = resolver.getLibrary(aid);
} catch (e) {}
if (lib != null && Utils.isNotEmpty(lib.name)) {
// all objects within the file
lib.units.forEach((CompilationUnitElement unit) {
// only the types, not methods
unit.types.forEach((ClassElement el) {
// only the ones annotated
if (el.metadata.any((ElementAnnotation ea) =>
ea.computeConstantValue().type == exposed)) {
// use it
}
});
});
}
});
return '''
$libName
''';
}
}
It seems what you want is what this issue is about How to generate one output from many inputs (aggregate builder)?
[Günter]'s answer helped me somewhat.
Buried in that thread is another thread which links to a good example of an aggregating builder:
1https://github.com/matanlurey/build/blob/147083da9b6a6c70c46eb910a3e046239a2a0a6e/docs/writing_an_aggregate_builder.md
The gist is this:
import 'package:build/build.dart';
import 'package:glob/glob.dart';
class AggregatingBuilder implements Builder {
/// Glob of all input files
static final inputFiles = new Glob('lib/**');
#override
Map<String, List<String>> get buildExtensions {
/// '$lib$' is a synthetic input that is used to
/// force the builder to build only once.
return const {'\$lib$': const ['all_files.txt']};
}
#override
Future<void> build(BuildStep buildStep) async {
/// Do some operation on the files
final files = <String>[];
await for (final input in buildStep.findAssets(inputFiles)) {
files.add(input.path);
}
String fileContent = files.join('\n');
/// Write to the file
final outputFile = AssetId(buildStep.inputId.package,'lib/all_files.txt');
return buildStep.writeAsString(outputFile, fileContent);
}
}
I created a const object at app.config.dart with the following code:
const configObj = const {
'webServer': const {
'appBaseHref' : "/"
},
'auth0': const {
'apiKey': "foo",
'domain': "bar",
'callbackUrl': "callback"
}
};
now in my main dart file I import the app.config.dart and I try to get the values there and now idea how to do that. configObj.auth0.apiKey produces the error EXCEPTION: Class 'ImmutableMap' has no instance getter 'auth0'.
so how do I do this ?
thanks!
Dart doesn't support to access map entries with .
It should be:
configObj['auth0']['apiKey'];
Alternatively you can create classes for your configuration like
class WebServerConfig {
final String appBaseHref;
const WebServerConfig(this.appBaseHref);
}
class Auth0Config {
final String apiKey;
final String domain;
final String callbackUrl;
const Auth0(this.apiKey, this.domain, this.callbackUrl);
}
class MyConfig {
final WebServerConfig webServer;
final Auth0Config auth0;
const MyConfig(this.webServer, this.auth0);
}
const configObj = const MyConfig(
const WebServerConfig("/"),
const Auth0Config(
"foo",
"bar",
"callback"
)
);
This way you also get proper auto-completion when you access the config properties and can use the simple . notation to access properties.