Can't copy image to assets folder on real device flutter - ios

When I select an image from gallery on iPhone, i want to copy it into the assets/images folder and display it afterwards.
On Simulator everything works fine. But on real device I get the following error:
Runner[44762:20816132] [VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: FileSystemException: Cannot copy file to '../../assets/images/image_picker_39C262A3-37A8-4167-8A9A-809C05C6DA67-44762-000025ACD7154E4A.jpg', path = '/private/var/mobile/Containers/Data/Application/4691143F-EC0F-470D-B7EC-88A9BCF021F8/tmp/image_picker_39C262A3-37A8-4167-8A9A-809C05C6DA67-44762-000025ACD7154E4A.jpg' (OS Error: No such file or directory, errno = 2)
#0 _File.copy.<anonymous closure> (dart:io/file_impl.dart:328:9)
#1 _rootRunUnary (dart:async/zone.dart:1434:47)
#2 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
<asynchronous suspension>
#3 _ProductInputPageState.getFromGallery (package:outfitcreator/pages/product_input_page.dart:451:9)
<asynchronous suspension>
I put the folder into the pubspec.yaml.
flutter:
assets:
- assets/images/
Do you have any suggestion how to encounter this?
Thank you very much!

You cannot copy files to your asset folder in runtime. After you pick the image you can save it to the temp dorectory or any other device folders like this
final Directory docDir = await getApplicationDocumentsDirectory();
final String localPath = docDir.path;
File file = File('$localPath/${path.split('/').last}');
final imageBytes = await rootBundle.load(path);
final buffer = imageBytes.buffer;
await file.writeAsBytes(
buffer.asUint8List(imageBytes.offsetInBytes, imageBytes.lengthInBytes));

Related

Can't run any other flutter app except for one

I've cloned a new flutter project from git
When I try to run I get the following error
Error: Could not resolve the package 'application_flutter_todo' in 'package:application_flutter_todo/utils/constants/api.dart'.
../../.pub-cache/git/flutter_maximo_api-35d1fef13c59a0e7fb70a20916adf1f9d26eac02/lib/src/maximo_api.dart:5:8: Error: Not found:
'package:application_flutter_todo/utils/constants/api.dart'
import 'package:application_flutter_todo/utils/constants/api.dart';
^
Unhandled exception:
FileSystemException(uri=org-dartlang-untranslatable-uri:package%3Aapplication_flutter_todo%2Futils%2Fconstants%2Fapi.dart; message=StandardFileSystem only supports file:* and data:*
URIs)
#0 StandardFileSystem.entityForUri (package:front_end/src/api_prototype/standard_file_system.dart:33:7)
#1 asFileUri (package:vm/kernel_front_end.dart:604:37)
<asynchronous suspension>
#2 writeDepfile (package:vm/kernel_front_end.dart:799:21)
<asynchronous suspension>
#3 FrontendCompiler.compile (package:frontend_server/frontend_server.dart:472:15)
<asynchronous suspension>
#4 _FlutterFrontendCompiler.compile (package:flutter_frontend_server/server.dart:38:22)
#5 starter (package:flutter_frontend_server/server.dart:149:27)
#6 main (file:///b/s/w/ir/cache/builder/src/flutter/flutter_frontend_server/bin/starter.dart:8:30)
#7 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:305:32)
#8 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
Failed to build bundle.
Failed to package /Users/Mohammad/Gemba/application_traxxion.
Could not build the precompiled application for the device.
It appears that your application still contains the default signing identifier.
Try replacing 'com.example' with your signing id in Xcode:
open ios/Runner.xcworkspace
Note that the current project doesn't have the import 'package:application_flutter_todo' statement.
That statement belongs to another app and is not in the current project that I'm trying to run.
I've tried,
1- flutter clean
2- updating flutter
3- updating packages
4- cleaning the product
5- cleaning the project
6- running the project from terminal, visual studio code, xcode all three gave the same result.
Nothing of the above have worked.
As the error implies, you should change your package name and BundleId for Android & iOS respectively. You shouldn't be using the default ones.
You can find how to do so here.

puppeteer says Exception: Websocket url not found

For the first time, I am using puppeteer with dart , here the simple code :
import 'package:puppeteer/puppeteer.dart';
main() async {
var browser = await puppeteer.launch();
await browser.close();
}
In pubspec yaml file:
dependencies:
puppeteer: ^1.16.1
websocket: ^0.0.5
# path: ^1.6.0
when I am trying to run this piece of code with dart command:
dart puppy1_test.dart
I am getting this exception :
Unhandled exception:
Exception: Websocket url not found
#0 _waitForWebSocketUrl (package:puppeteer/src/puppeteer.dart:311:3)
<asynchronous suspension>
#1 Puppeteer.launch (package:puppeteer/src/puppeteer.dart:170:30)
<asynchronous suspension>
#2 main (file:///D:/project/<removed>/dl/puppy1/test/puppy1_test.dart:7:33)
#3 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:307:19)
#4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
I solved the problem by deleting the .local-chromium folder. In this folder the chrome application is stored.
I suspect that the reason was a version mismatch, because I updated the dart version, after the creation of the project.

How to fix "flutter: DatabaseException(open_failed...)"?

I'm trying to run my flutter app on my iPhone 7 but I'm receiving an error message. Running the app on my Android phone as well as on the iPhone XR simulator works fine.
I also tried to test the app on my iPad and I got the exact same error message as shown below. Replacing the ios folder with a fresh copy did not solve the issue.
I've tried to run the latest sqflite version and flutter doctor shows no issues.
Future<Database> initDatabase() async {
//Get the dir
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + 'posts.db';
//Open or Create the database using given path
var postsDataBase = openDatabase(path, version: 1, onCreate: _createDb);
return postsDataBase;
}
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: DatabaseException(open_failed /var/mobile/Containers/Data/Application/04C0A23F-6C36-42C2-9CE5-C7C5370F59FA/Documentsposts.db)
#0 wrapDatabaseException
package:sqflite/src/exception_impl.dart:11
<asynchronous suspension>
#1 SqfliteDatabaseFactoryImpl.wrapDatabaseException
package:sqflite/src/factory_impl.dart:29
#2 SqfliteDatabaseMixin.safeInvokeMethod
package:sqflite/src/database_mixin.dart:184
#3 SqfliteDatabaseMixin.openDatabase
package:sqflite/src/database_mixin.dart:519
<asynchronous suspension>
#4 SqfliteDatabaseMixin.doOpen
package:sqflite/src/database_mixin.dart:612
<asynchronous suspension>
#5 SqfliteDatabaseOpenHelper.openDatabase
package:sqflite/src/database.dart:32
<asynchronous suspension>
#6 SqfliteDatabaseFactoryMixin.openDatabase.<anonymous closure>
package:sqflite/src/factory_mixin.dart:100
<asynchronous suspension>
#7 ReentrantLock.synchronized.<anonymous closure>.<anonymous closure>
package:synchronized/src/reentrant_lock.dart:33
#8 _rootRun (dart:async/zone.dart:1124:13)
Check whether the parent directory exists or not and try using the latest sqlite version.
This worked for me.
String path = directory.path + '/' + 'posts.db';
As you can see in the logs, the path you build ends with /Documentsposts.db where I think you likely want /Documents/posts.db
Don't use the plus sign to build a path but use join from the package path.
i.e. instead of:
String path = directory.path + 'posts.db';
do
var path = join(directory.path, 'posts.db');
On iOS you should also make sure the parent directory exists first.

Creating a directory list in windows using Dart

I am trying to create a directory list on windows using dart but I get an error
Here is my code
void listerine(Directory pat){
//Directory root = new Directory(pat);
//var root = Directory.systemTemp;
pat.list(recursive: true, followLinks: false).listen((FileSystemEntity entity){
main(List<String> arguments){
var root = new Directory("C:\\");
print(root);
listerine(root);
}
Btw I am using android studio as an IDE.
I gives me this error.
I/flutter ( 7224): Directory: 'C:\'
E/flutter ( 7224): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 7224): FileSystemException: Directory listing failed, path = 'C:\/' (OS Error: No such file or directory, errno = 2)
E/flutter ( 7224): #0 _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:1114:29)
E/flutter ( 7224): #1 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 7224): #2 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
You have the question tagged as flutter but seems you are asking about a pure Dart program?
This code
import 'dart:io';
void listerine(Directory pat) async {
await for (var v in pat.list()) {
print(v);
}
}
main() {
listerine(new Directory("C:\\"));
}
produces what you'd expect on Windows, namely
Directory: 'C:\$Recycle.Bin'
Directory: 'C:\$WINDOWS.~BT'
Directory: 'C:\Apps'
...
Flutter uses a modified version of the Dart SDK replacing html support with the mobile rendering engine (skia).
The majority of Dart code can run in both versions, but not all. This means that you can re-use packages in Dart VM applications and in Flutter applications - but again not all. In particular, interacting with the browser or the platform are only available in their respective versions.
To interact with the Windows file system, you need the Dart VM version. Download this separately. As Günter says, to interact with the mobile file system in Flutter, use path_provider.

PWA Worker throws an error with AngularDart

I am trying to add offline capabilities to an angular dart project, running pub build but
Running pub build:pub.exe build
-----
Unhandled exception:
ProcessException: The system cannot find the file specified.
Command: pub.exe build
#0 _buildProjectIfEmptyOrOld (http://localhost:52940/pwa.dart:116)
<asynchronous suspension>
#1 main (http://localhost:52940/pwa.dart:40)
<asynchronous suspension>
#2 _startIsolate.<anonymous closure> (dart:isolate-
patch/isolate_patch.dart:263)
#3 _RawReceivePortImpl._handleMessage (dart:isolate-
patch/isolate_patch.dart:151)
main.dart
import 'package:angular/angular.dart';
import 'package:my_app/views/app_component/app_component.dart';
import 'package:pwa/client.dart' as pwa;
void main() {
bootstrap(AppComponent);
// register PWA ServiceWorker for offline caching.
new pwa.Client();
}
pubspec.yaml
dependencies:
pwa: ^0.1.2
At that point, the pwa is trying to run pub.exe build. I haven't tried it on Windows, only a friend reported it working, but it is entirely possible that pub.exe does no longer work (if it ever did), and it should have been pub all along (and in this case it is a Windows-related bug).
Suggested workaround:
run pub build
run pub run pwa
run pub build again
If the above works, let me know, and I'll fix it in a patch release.

Resources