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.
Related
Is anyone know how to call JS function from dart ?
I followed steps which linked below.
Firstly, appear this error.
"Error: Not found: 'dart:js' import 'dart:js';"
I followed previous answer which comment this line from js.dart and fixed it.
[export 'dart:js' show allowInterop, allowInteropCaptureThis;].
But now I got another error below.
No top-level method 'callJsFunc' declared. Receiver: top-level Tried calling: callJsFunc()
Is anyone know how to fix this error ?
text
I tried other Flutter SDK version (latest one and 3.0.5) but got same error.
flutter uses dart not javascript.
so... actually you require a separate javascript engine if you want to use javascript with flutter and that can be done with dart ffi unless you are building a web app with flutter.
However, there is a library called flutter_js and you can use it if you need to run javascript.
On the other hand, if you are interested in using embeddable Javascript engine with your flutter project by yourself.
you should check this link out where it shows how to use ffi for attaching an embeddable Javascript engine called Duktape
final List image = await imageLabeler.processImage(inputImage); // <---Error Exception
I am using an android.
I have seen a person with the same exact issue, but they were running on the web, and it is not implemented to run on the web. I am using an android, so there should be no problems.
Is there a way to fix this issue?
import 'package:google_ml_kit/google_ml_kit.dart';
import 'package:google_mlkit_image_labeling/google_mlkit_image_labeling.dart';
import 'package:google_mlkit_commons/google_mlkit_commons.dart';
final List image = await imageLabeler.processImage(inputImage);
the example shown above was taken from a youtube video, word for word, so there should be zero problems.
https://www.youtube.com/watch?v=giVk7yGUkas&t=244s&ab_channel=LearnwithJahangirJadi
EDIT
i tried closing editor/ doing a flutter clean/ and closing my app, I encountered another proble
Exception has occurred.
FirebaseException ([cloud_firestore/unavailable] The service is currently unavailable. This is a most likely a transient condition and may be corrected by retrying with a backoff.)
adding these to the pubspec.yaml files causes this error
google_ml_kit:
google_mlkit_image_labeling:
google_mlkit_commons:
removing them fixes the error.
what am I doing wrong?
Sometimes this happens to me, try to run these steps:
flutter clean
flutter pub get
close your code editor, your app/emulator, and try again.
There are three types that are present on the lib 'package:flutter/foundation.dart' that I need to use in an environment without Flutter. They are: ByteData, ReadBuffer and WriteBuffer. Is it possible to use these classes without having the whole Flutter as dependency?
The reason why I need to use it in an environment without Flutter is because I have a server made using Dart and I need to use these classes to encode/decode my socket messages.
Thanks in advance!
If you want access to classes in package:flutter/foundation.dart you can create a new flutter project as normal and remove runApp() from main() like this:
void main() {
print("This is now a console only program")
//no runApp(new MaterialApp())
}
This code won't start any mobile app and doesn't need an emulator, just a console. I have done some projects only with Dart and you can use all non UI Material Classes, such as File, StringBuffer...
Note: Remember to add the path for dart in the system environment variables, in my case D:\APPS\flutter\bin\cache\dart-sdk\bin, then you can navigate to your flutter project and type dart lib/main.dart in the console to run the code
This is the similar question (with wrong title): Flutter Web - How to reload currently Active Page
Running the Flutter web app as PWA I need to allow users to initiate refresh and thus update the (web) app.
Unfortunately import 'dart:html' as html; is not allowed anymore when having the same codebase for web and mobile native apps. So following code is not the option:
RaisedButton(
child: Text("Update me"),
onPressed: () {
if (kIsWeb) html.window.location.reload();
},
),
What is the correct approach?
EDIT:
I have managed to use 'dart:js' in the same codebase for pwa and mobile using conditional imports. This means I can call JavaScript from within the Flutter code. Unfortunately location.reload(true); does not reload the PWA.
Ideally I would have the Flutter approach for the PWA reload / update or JavaScript workaround.
EDIT2:
The whole issue is within the PWA handling of refresh button / window reload.
Unfortunately service worker's skipWaiting() can be called only from within service worker ( https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#skip_the_waiting_phase )
The correct approach seams to be sending the skipWaiting message to the new instance of the service worker.
However skipWaiting is not yet fully supported on some browsers (iOS?) so the safer approach seams to be just unregistering the worker...
I ended up using following to stop the worker and reload.
if ('serviceWorker' in navigator) {
navigator.serviceWorker.getRegistration().then(swr => {swr.unregister().then(
() => {location.reload(true)}
)});
} else {location.reload(true)}
Related:
Access service worker skipWaiting from within App build with Webpack+Workbox
https://medium.com/#nekrtemplar/self-destroying-serviceworker-73d62921d717
https://redfin.engineering/how-to-fix-the-refresh-button-when-using-service-workers-a8e27af6df68
https://deanhume.com/displaying-a-new-version-available-progressive-web-app/
Flutter 2.10.3
In the newest version of Flutter (2.10.3 at the time of writing this), PWAs will automatically update when the serviceWorkerVersion changes in your index.html file.
From this issue, Jonah Williams comments:
"The app will update after the new version has been downloaded and the page revisited."
You can read more about general PWA support by Flutter here. For the pull request that added server worker support, see this.
If you want to allow users to manually fetch a new version of your app, you can use the universal_html package which works on all platforms.
import 'package:universal_html/html.dart' as html;
onTap: () {
html.window.location.reload();
},
Keep an eye on this issue for an improvement to how server worker caching is handled.
A work around would be to navigate to your apps entry point route '/'
`Navigator.of(context).pushNamedAndRemoveUntil(
'/,
(Route<dynamic> route) => false
);`
I maintain a set of web versions myself. When I upgrade my web application, I will upgrade the version level. Every time I start homePage, I will request the version number of the remote configuration to match the version number of the current PWA application. When the local version number is lower than the remote version number, I will refresh
//pubspec.yaml universal_html: ***
import 'package:universal_html/html.dart' as html;
getWebVersion() async{
var res= await dio.get(web_version);
if (locaVersion < res.version) {
html.window.location.reload();
}
}
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("*.*");