I want to automatically test a flutter app which uses a platform plugin and need to access the adb logcat output programmatically.
Is there a way to parse the log output from dart and check if some text was printed, e.g. after a button was pressed?
I am using Android studio to develop my app.
Dart's print() outputs the system console, you can also use the command flutter logs.
according to flutter website flutter offical docs :
"The Dart print() function outputs to the system console, which you can view using flutter logs (which is basically a wrapper around adb logcat)."
Check the plugin called logcat_monitor on pub.dev.
Its biggest advantage over the other logcat plugin is that it allows continuous monitoring of logcat messages.
how to use
Add the dependencies:
dependencies:
logcat_monitor: ^0.0.4
Create a function to consume the logcat messages
void _mylistenStream(dynamic value) {
if (value is String) {
_logBuffer.writeln(value);
}
}
Register your function as a listener to get logs then use it in anyway within your app.
LogcatMonitor.addListen(_mylistenStream);
Start the logcat monitor passing the filter parameters as defined in logcat tool.
await LogcatMonitor.startMonitor("*.*");
Related
Flutter run and build commands accept the --flavor parameter. I have found a tutorial on implementing it in Android and iOS projects. And, I also found a way to access the flavor name from the flutter code.
But I still have no idea how to access the flavor name on other platforms. It seems flutter documentation has no information about it.
Is there a way to read the --flavor parameter value from the flutter code on any platform?
The reason for my question in the comments is that you might be looking for using the --dart-define flag when building your app, instead of --flavor.
They serve different purposes, so both might be required depending on your desired outcome.
The value passed via the dart define flag can be accessed in the code via String.fromEnvironment(...)
Is it possible to write a flutter code and save it inside server and then send it to a flutter application and implement this code .. Is this possible?
You can take a look at this:
https://pub.dev/packages/flutter_code_push
I tried a lot of things and got this package, but I couldn't use it on Windows devices.
flutter_code_push 1.0.2
Might be what you want.
Isolate.spawnUri can run compiled code.
https://github.com/dart-lang/sdk/issues/50158
I'm writing a flutter app that needs to set up elasticsearch on first run. To do this, I need to run some commands in the terminal, I know how to do it, but I also need information from the terminal for a complete setup. How to get information from terminal to my flutter app, can i assign the value of one of the terminal strings to some variable in flutter?
I not found a some issues about getting info from terminal to app.
You can use --dart-define from the terminal while running your app or while building your application.
for example
flutter run --dart-define=BASE_URL=https://flutter.dev
Here BASE_URL is a key and you can pass value to it, you can name it as per your need.
And, You can access this in your Flutter app like below:
const String.fromEnvironment('BASE_URL')
You can check out this video as well for the same: Passing values from the command line to Flutter app
const is required while accessing that value you pass using dart-define and the key name is case sensitive.
Flutter Engine has a DartExecutor class, written in Java. It allows, for example, run a Dart callback from native Android code. Is there a similar way for Linux/Windows/macOS? I'm trying to organize a WorkManager-like behavior to run the callback in a separate main Isolate. This allows to run Flutter code inside this callback, unlike the usual Isolate.
fl_project allows to set custom Dart entry point arguments (then pass fl_project to fl_engine and run it), but I haven't found a way to replace the Dart entry point, while the internal API has this option https://engine.chinmaygarde.com/struct_flutter_project_args.html#aa7aab3cb3dc28d379b97adef882f52e4
I'm unable to get any error output from my Flutter web app. Printing to the console using
print('some text');
works fine, but no errors get printed. For example, throwing an exception
throw new Exception('testexception');
doesn't result in any output, neither in the browser console nor in IntelliJ. The log level settings in Chrome are set to [Info, Warnings, Errors].
I even tried implementing a custom error handler
void main(){
FlutterError.onError = (FlutterErrorDetails details) {
print('main.onError: details: ${details.toString()}');
};
runApp(new MyApp());
}
but no luck. Do I have to enable error outputs somewhere? I can't find any info about this in the documentation.
I tried running the app both using the Dart Dev Server (which is started when using Run from IntelliJ), as well as calling webdev serve and webdev serve --auto restart from the Terminal.
Flutter Web currently doesn't have a way to be debugged. Will generate a main.dart.js and you can debug it with Chrome console.
One cool trick to "debug" your Web App, is by showing a popup to your browser:
import 'dart:js' as js;
#override
void initState() {
super.initState();
js.context.callMethod("alert", <String>["Your debug message"]);
}
Did you look to see if it was a known issue? Flutter-web is only at developer preview level. If there's not a current issue, create one, and provide a minimal "how to reproduce". I'm sure the Flutter-web team would appreciate it.
I tried in both IDE and its correctly working in Intellij but not in VSCode. Its showing white blank screen give it try to run again flutter run -d chrome and wait in blank screen for 2minutes.
And check the SDK path of flutter in visual code is correct or not.
In JavaScript, the console.log method is used to output to the Web browser's JavaScript console.
So you can do it from Dart by getting the JavaScript console object and invoking that method on it.
import 'dart:js' as javascript; // ignore: avoid_web_libraries_in_flutter
/// Invoke the the JavaScript `console.log` method.
void logToBrowser(String message) {
final console = javascript.context['console'] as javascript.JsObject;
console.callMethod('log', [message]);
}
Since dart:js is only supported for Dart compiled to JavaScript, this means the Flutter app using it will only work as a Flutter Web app. Importing it will mean the code won't work for an app targeting iOS, Android, etc.
Tip: the JavaScript console object has other methods that can be invoked. Instead of log, use info, warn and error. Those last two highlight the entry when they appear in the console.