Error: Not found: 'dart:ffi' with flutter web
Asked Answered
S

9

13

Read carefully before reporting.

My flutter web project was working perfectly fine. A few hours later, it started complaining about 'dart:ffi: which i didn't even import. I saw similar questions but none of them were in my case. I tried everything I could find but nothing worked. Here are the errors:

/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/drift-1.7.1/lib/src/sqlite3/database_tracker.dart:1:8: Error: Not found: 'dart:ffi'
import 'dart:ffi';
       ^

/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/sqlite3-1.8.0/lib/src/ffi/api/database.dart:1:8: Error: Not found: 'dart:ffi'
import 'dart:ffi';
       ^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/sqlite3-1.8.0/lib/src/ffi/api/statement.dart:1:8: Error: Not found: 'dart:ffi'
import 'dart:ffi';
       ^

/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/sqlite3-1.8.0/lib/open.dart:5:8: Error: Not found: 'dart:ffi'
import 'dart:ffi';
       ^
...
                                          
Failed to compile application.
Exited (sigterm)

I couldn't include the entire debug console's output but these are the top and bottom lines. Please help, thank you.

Slippy answered 18/9, 2022 at 12:27 Comment(6)
you're using sqlite3 and it is not supported in web - pub.dev/packages/sqlite3Parchment
How do i get rid of it? Because I've never worked with it and it seems like there's a whole lot of files and folders with 'sqlite3' labelSlippy
are you using a package/plugin that is dependent on sqlite3?Parchment
I don't think so, tbh i've never heard about SQLite3 My code stopped working all of a suddenSlippy
I figured it out. I just deleted 'web_ffi' folder and it somehow worked.Slippy
anybody a real answer? Dealing with the same issue here! (onI get same error on different file (....../aes_decrypt.dart:1:8: Error: Not found: 'dart:ffi' import 'dart:ffi';)Agoraphobia
S
-6

I figured it out. I just deleted 'web_ffi' folder and it somehow worked.

Slippy answered 18/9, 2022 at 15:18 Comment(3)
I did not find web_ffi, where is it located?Yardarm
Ya, I am also not able to find 'web_ffi'Centri
also, I did not find 'web_ffi' folder.Ion
V
20

In my case, import 'dart:ffi' was added to my project automatically, probably by my IDE. I just searched with the search function of my IDE and I delete the import.

Venita answered 20/3, 2023 at 16:24 Comment(0)
I
5

Indeed, I also got this error. However, none of the previous solutions helped me. I spent hours and finally I understood that I was doing native imports in the context of the web. The application I made compiles on Android and on the Web. How did I do it?

Step 1: My dependency for both web and Android in my pubsec

#SQLITE with moore for all platforms
moor: ^4.6.0+1
moor_flutter: ^4.0.0
ffi: ^1.1.2
# for sqlite on android
sqlite3_flutter_libs:

Step 2: injecting my db object depending of the Platform.

My service_locator.dart is the file I use to inject all my dependencies in my project (Database, Rest Api Clients, etc.); your own may have another name.

This is the content of my service_locator.dart:

export 'unsupported.dart'
if (dart.library.ffi) 'service_locator_native.dart'
if (dart.library.html) 'service_locator_web.dart';

As you can see, if the platform is android it will import native code otherwise if it's web it will import library for the web.

This is the sample of service_locator_web.dart:

    import 'dart:async';    
    import 'package:get_it/get_it.dart';
    import 'package:my_app/src/dao/chat_channel_dao.dart';//database class
    import 'package:drift/web.dart';//only for the web
        
    final GetIt serviceLocator = GetIt.instance;    
    initLocator() {
    ...
    //Environment Settings
    ...    
     //DAO Injection
     serviceLocator.registerLazySingleton<ChatContactDao>(() => ChatChannelDao(WebDatabase('db_chat_channels')));
     ...
    }
    //to fake the interpreter to see service_locator like an getIt Instance
    T get<T extends Object>() {
      return serviceLocator.get<T>();
    }
    
    registerLazySingleton<T extends Object>(T Function() function){
      serviceLocator.registerLazySingleton<T>(function);
    }

FutureOr unregister<T extends Object>({
  Object? instance,
  String? instanceName,
  FutureOr Function(T)? disposingFunction,
}){
  return serviceLocator.unregister<T>();
}

This is the sample of service_locator_native.dart:

import 'dart:async';    
import 'package:get_it/get_it.dart';
import 'package:my_app/src/dao/chat_channel_dao.dart';//database class
import 'package:drift/native.dart';//only for the native platform
    
final GetIt serviceLocator = GetIt.instance;
    
initLocator() {
...
//Environment Settings
...
//DAO Injection
serviceLocator.registerLazySingleton<ChatContactDao>(
          () => ChatChannelDao(makeLazyDb('db_chat_channels.sqlite')));//here for native DB
...
}

//to fake the interpreter to see service_locator like an getIt Instance
T get<T extends Object>() {
  return serviceLocator.get<T>();
}
registerLazySingleton<T extends Object>(T Function() function){
  serviceLocator.registerLazySingleton<T>(function);
}
// to call native Db Builder
LazyDatabase makeLazyDb(String dbName){
final db = LazyDatabase(() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, dbName));
return NativeDatabase(file);
});
  return db;

}

For unsupported platform this is the code of unsupported.dart:

import 'dart:async';

T get<T extends Object>()  => throw UnimplementedError();
initLocator() => throw UnimplementedError();
registerLazySingleton<T extends Object>(T Function() function)=> throw UnimplementedError();


FutureOr unregister<T extends Object>() => throw UnimplementedError();

Step 3: calling my DB class

   import 'package:my_app/src/service/service_locator.dart' as serviceLocator;
    class my_sample_ui{
    my_sample_method(EntityChatChannel myEntity) async{
    var ticketDao = serviceLocator.get<TicketDao>();
        await chatChannelDao?.createNew(myEntity);
    }
//implements your own methods
}

Conclusion: This is simply a design pattern to assist you all, which can be adapted based on your code structure. In this example, I am using GetIt for dependency injection between classes. Flutter is an exceptional multi-platform framework, but we need to write multi-platform code to ensure it works in all environments.

Indenture answered 23/4, 2023 at 14:39 Comment(0)
J
3

I just encountered this error and removed all unused imports in all of my widget files and error stopped appearing. I hope this solves your issue.

Janina answered 17/1, 2023 at 13:40 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Acidosis
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewMariko
A
0

You can delete the import 'dart:ffi'; by hovering over

/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/drift-1.7.1/lib/src/sqlite3/database_tracker.dart:1:8: Error: Not found: 'dart:ffi'

and then pressing the ctrl button on windows and then clicking your left mouse button, this will take you to the location of the error and on the top you will find the import 'dart:ffi' remove it or comment on it.

Accentor answered 15/11, 2022 at 12:33 Comment(0)
E
0

Type on terminal flutter install dart:ffi if you get this error I think it will help you

Euphonium answered 9/2, 2023 at 15:26 Comment(1)
Hello, the answer is incorrect, dart:ffi didn't need to be install as an app on a device.Maeganmaelstrom
C
0

i dont know if it works for all scenarios but i also just deleted the 'dart:ffi' import and it worked

Cloudless answered 3/7, 2023 at 17:30 Comment(0)
A
0

For web version you must use connection like this

import 'package:drift/drift.dart';
import 'package:drift/wasm.dart';
import 'package:flutter/foundation.dart';

part 'app_database.g.dart';

@DriftDatabase(tables: [YOUR_TABLE_NAME])
class AppDatabase extends _$AppDatabase {
  AppDatabase() : super(_connect());

  @override
  int get schemaVersion => 1;
}

DatabaseConnection _connect() {
  return DatabaseConnection.delayed(Future(() async {
    final db = await WasmDatabase.open(
      databaseName: 'YOUR_DB_NAME',
      sqlite3Uri: Uri.parse('sqlite3.wasm'),
      driftWorkerUri: Uri.parse('drift_worker.js'),
    );

    if (db.missingFeatures.isNotEmpty) {
      debugPrint('Using ${db.chosenImplementation} due to unsupported '
          'browser features: ${db.missingFeatures}');
    }

    return db.resolvedExecutor;
  }));
}

and use this for mobile version:

import 'dart:io';

import 'package:drift/native.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';

// ... the TodoItems table definition stays the same

@DriftDatabase(tables: [TodoItems])
class AppDatabase extends _$AppDatabase {
  AppDatabase() : super(_openConnection());

  @override
  int get schemaVersion => 1;
}

LazyDatabase _openConnection() {
  // the LazyDatabase util lets us find the right location for the file async.
  return LazyDatabase(() async {
    // put the database file, called db.sqlite here, into the documents folder
    // for your app.
    final dbFolder = await getApplicationDocumentsDirectory();
    final file = File(p.join(dbFolder.path, 'db.sqlite'));

    // Also work around limitations on old Android versions
    if (Platform.isAndroid) {
      await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
    }

    // Make sqlite3 pick a more suitable location for temporary files - the
    // one from the system may be inaccessible due to sandboxing.
    final cachebase = (await getTemporaryDirectory()).path;
    // We can't access /tmp on Android, which sqlite3 would try by default.
    // Explicitly tell it about the correct temporary directory.
    sqlite3.tempDirectory = cachebase;

    return NativeDatabase.createInBackground(file);
  });
}

check this and this for more info

Apperception answered 7/12, 2023 at 19:10 Comment(0)
F
0

For me i just find where the dart:ffi was imported and deleted that, and now my web is working fine again

Fredericton answered 30/4 at 9:43 Comment(0)
S
-6

I figured it out. I just deleted 'web_ffi' folder and it somehow worked.

Slippy answered 18/9, 2022 at 15:18 Comment(3)
I did not find web_ffi, where is it located?Yardarm
Ya, I am also not able to find 'web_ffi'Centri
also, I did not find 'web_ffi' folder.Ion

© 2022 - 2024 — McMap. All rights reserved.