5

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.

Mamoudou Kane
  • 141
  • 2
  • 7

7 Answers7

6

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.

Spixz
  • 131
  • 2
  • 8
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.

  • 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](/help/how-to-answer). – Community Jan 18 '23 at 06:23
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33649702) – V-rund Puro-hit Jan 24 '23 at 10:13
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.

Noor
  • 2,071
  • 19
  • 28
0

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

0

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.

0

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

-4

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

Mamoudou Kane
  • 141
  • 2
  • 7