How to setup different firebase environments(Flavors) in Flutter Web - flutter

I am trying to set up different firebase environments(for example :qa,prod) in a flutter project.
I created two projects in firebase, one for production, one for the qa. Then, in an iOS or Android project, I using separate google-services.json or GoogleServices-Info.plist files.
but,how to do it in Flutter web?

If you need this dynamic config in dart code, you can create an interface that represents your configs and create implementations for each environment, so you can change the implementation passing a parameter when run flutter app (using --dart-define).
Imagine the case that you have a prod and an homolog environment, you can create three files:
The first one is the contract of your configs, that all environments must have implemented.
abstract class AppConfig {
String get baseUrl;
}
And we create a class for each environment, with the implementation of the configs defined in AppConfig.
class ProdConfig implements AppConfig {
#override
String get baseUrl => 'https://my-prod-url.com';
}
class HomologConfig implements AppConfig {
#override
String get baseUrl => 'https://my-homolog-url.com';
}
When we need to get the config class, we instantiate based on parameter passed by --dart-define
AppConfig getConfig() {
const environmentParameter = String.fromEnvironment('ENV');
switch (environmentParameter) {
case 'prod': return ProdConfig();
case 'homolog': return HomologConfig();
default: throw Error(); // You can set a default environment
}
}
And when you run your app, you just need to pass this parameter, like the example below:
flutter run --dart-define ENV=prod
Or in homolog:
flutter run --dart-define ENV=homolog
BUT, if you need to set configs like firebase, you should check this answer

Related

How can I apply dart extension methods in the Flutter Logging library?

I am trying logging library in my flutter project. They have some good record messages (e.g. record.name, record.message, etc.) For my project, I want to extend by using dart extension methods this package to add some more record message (e.g. record.version, record.eventName, etc.). As a beginner in dart, I am not sure completely how to do that?
Here is an example that I tried but failed.
extension CustomLog on LogRecord {
String version(LogRecord version) => "1.0";
String eventName(LogRecord eventName) => "userNameChangedEvent";
}
Please provide me some suggestions/examples how can I extend any package and use it on my own.
Logging does not support extending (with extension) the LogRecord to add custom fields.
You can pass all your custom info as an class object in object param and retrieve it later.
Something like
class CustomLogAttributes {
final String version;
final String eventName;
CustomLogAttributes(this.version, this.eventName);
}
then
log.fine("example log message", object: CustomLogAttributes("1.1.0", "example_event");

How to ignore package when building flutter project for web?

I have a project which uses flutter_libserialport library on macOS.
I am modifying it to work on web however this library does not work on web.
I am building a web implementation using navigator.serial in javascript which works fine.
However when I attempt to build the project for web I get the following error
/opt/homebrew/Caskroom/flutter/2.2.3/flutter/.pub-cache/hosted/pub.dartlang.org/libserialport-0.2.0+3/lib/src/config.dart:25:8: Error: Not found: 'dart:ffi'
import 'dart:ffi' as ffi;
This makes sense since FFI is not available on web.
But I don't even need the libserialport library on web any way.
How can I get flutter to ignore it?
I tried this however it doesn't contain information on how to exclude a package.
It also does not contain information on how to ignore it specifically for web. It seems to just ignore it in general.
Maybe you should guard your usages of libserialport with the kIsWeb predicate like following:
if(!kIsWeb){
// libserialport code execution here
}
I searched a lot as well and didn't find a way you can do that, I think this should be handled by the package itself not the package's users like in path_provider for instance.
As a workaround I have created a dummy libserialport's SerialPort class for web only as follows:
dummy_serialport.dart:
class SerialPort {
final String name;
static List<String> availablePorts = ['dummy'];
static SerialPortError? lastError;
SerialPort(this.name);
bool openReadWrite() {
return false;
}
}
class SerialPortError {}
// add more properties and functions as needed
main.dart:
import 'package:libserialport/libserialport.dart'
if (dart.library.html) './dummy_serialport.dart'
if (dart.library.io) 'package:libserialport/libserialport.dart';
....
if (!kIsWeb) {
final name = SerialPort.availablePorts.first;
final port = SerialPort(name);
if (!port.openReadWrite()) {
print(SerialPort.lastError);
exit(-1);
}
}
....
....
It's bad, I know :( but it works! maybe you can contact the package author to get more insight and if opening a PR where the interfaces are separated from the FFI implementation so that importing the classes wouldn't break web or something.

How to modify class name when creating a Flutter plugin

I'm creating a new Flutter plugin
I named it my_flutter_plugin when I created it. But now I want to change the main class name.
Currently it is this:
// lib/my_flutter_plugin.dart
import 'dart:async';
import 'package:flutter/services.dart';
class MyFlutterPlugin {
static const MethodChannel _channel =
const MethodChannel('my_flutter_plugin');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
}
I'd like to change MyFlutterPlugin to AnotherName, but when I look in pubspec.dart it says:
# The following section is specific to Flutter.
flutter:
# This section identifies this Flutter project as a plugin project.
# The androidPackage and pluginClass identifiers should not ordinarily
# be modified. They are used by the tooling to maintain consistency when
# adding or updating assets for this project.
plugin:
androidPackage: com.example.my_flutter_plugin
pluginClass: MyFlutterPlugin
I want to keep the package name as my_flutter_plugin but this seems to indicate that I can't (or shouldn't) change the plugin class name.
How do I change the class name?
The pluginClass in pubspec.yaml is not the same as your class with the method channel. In fact, if you had named your project my_flutter instead of my_flutter_plugin, the pluginClass in pubspec.yaml would still say myFlutterPlugin but your lib class with the method channel would be MyFlutter.
How to change the class name
In your lib folder class (in this case lib/my_flutter_plugin.dart), just refactor the class name as you would normally rename any class. You can rename it to AnotherName. As long as you are refactoring using the IDE tools, this should also update the class name in the example and test folders.
How to change the method channel name
Just change the string name everywhere that it is used. In a new plugin project, this would be in the lib folder and the test folder. Rather than
static const MethodChannel _channel =
const MethodChannel('my_flutter_plugin');
You can give it a unique name like
static const MethodChannel _channel =
const MethodChannel('com.example.my_flutter_plugin/another_name');

How to use classes like RegistryRoot correctly in custom actions?

I have to implement a custom action to search the windows registry for the installed version of the dotnet framework. Therefore I thought to extend the ReadRegistryValueAction to integrate my individual search algorithm. But the custom action will not be found at the IDE. So I extends the action from the AbstractInstallAction and included the RegistryRoot class to configure the action inside the IDE the same way as with provided registry actions of install4j framework.
public class CheckDotNetInstallationAction extends AbstractInstallAction {
private RegistryRoot registryRoot;
public RegistryRoot getRegistryRoot() {
return registryRoot;
}
public void setRegistryRoot(RegistryRoot registryRoot) {
this.registryRoot = registryRoot;
}
#Override
public boolean install(InstallerContext paramInstallerContext)
throws UserCanceledException {
// do custom search
return false;
}
}
But instead to get a dropdown list, there is only a blank field. I expected also a dropdown list the same way as in the present registry action. Now there are two questions:
Is it possible to extends existing actions/screens/forms and to use and configure it in the IDE or is it necessary to extends from the AbstractInstallAction?
How can I use classes like RegistryRoot for my custom components the same way as they are used in the actions provided by the install4j framework? Specifically the way to configure these components inside the IDE.
You have to add add a BeanInfo class and set an enumeration mapper. See the source file
samples/customCode/SampleActionBeanInfo.java
in your install4j install4j Installation and and look for the the call to setEnumerationMappers.

SpringApplicationContextLoader ignores Application class

The SpringApplicationContextLoader assumes that the application is either using 100% XML or 100% Java config. This is because #ContextConfiguration allows either a list of classes or locations/value, not both. If any is specified, SpringApplicationContextLoader ignores the Application class that creates and starts the SpringApplication.
Trying to make Boot work with a 100% Groovy/no-XML pet project, I ran across the above issue. My Application class has #EnableAutoConfiguration and #ComponentScan annotations on it, the former required by Boot to set up a Web server. The later I had to keep because of SPR-11627. On the other hand, if I omitted the locations/value on #ContextConfiguration, dependencies weren't set up (duh!).
I give the code below along with a patch that I locally made to SpringApplicationContextLoader. If there's a better way, please let me know.
MovieDatabaseRESTClientIntegrationTest.groovy
RunWith(SpringJUnit4ClassRunner)
#ContextConfiguration(value = ['classpath:client-config.groovy', 'classpath:integ-test-config.groovy'],
loader = PatchedSpringApplicationContextLoader)
#SpringApplicationConfiguration(classes = MovieDatabaseApplication)
#WebAppConfiguration
#IntegrationTest
class MovieDatabaseRESTClientIntegrationTest {
MovieDatabaseApplication.groovy
#EnableAutoConfiguration
#ComponentScan
class MovieDatabaseApplication {
SpringApplicationContextLoader.java fix
private Set<Object> getSources(MergedContextConfiguration mergedConfig) {
Set<Object> sources = new LinkedHashSet<Object>();
sources.addAll(Arrays.asList(mergedConfig.getClasses()));
sources.addAll(Arrays.asList(mergedConfig.getLocations()));
/* The Spring application class may have annotations on it too. If such a class is declared on the test class,
* add it as a source too. */
SpringApplicationConfiguration springAppConfig = AnnotationUtils.findAnnotation(mergedConfig.getTestClass(),
SpringApplicationConfiguration.class);
if (springAppConfig != null) {
sources.addAll(Arrays.asList(springAppConfig.classes()));
}
if (sources.isEmpty()) {
throw new IllegalStateException(
"No configuration classes or locations found in #SpringApplicationConfiguration. "
+ "For default configuration detection to work you need Spring 4.0.3 or better (found "
+ SpringVersion.getVersion() + ").");
}
return sources;
}
Also posted on Spring forum.
I could be wrong but I don't think there is any support for beans{} configuration in #ContextConfiguration and #SpringContextConfiguration is just an extension of that. A feature request in JIRA would be appropriate. Also there has never been any support for mixed configuration format (as the entry point at least) - you always have to choose either XML or #Configuration, or else supply your own ContextLoader. You also shouldn't have both #ContextConfiguration and #SpringContextConfiguration on the same class (the behaviour is undefined).