FlutterSQLiteSample
Sample app that exposes SQLite to flutter/dart via flutter services.
Install / Use
/learn @ripple182/FlutterSQLiteSampleREADME
FlutterSQLiteSample
Sample app that exposes SQLite to flutter/dart via flutter services.
Install Steps
- Setup your app to be an embedded FlutterView, see hello_services app
- copy
RawSQLiteHelper.javato the same package as your main Android Activity - setup your activity to look something like this:
private RawSQLiteHelper sqLiteHelper; private FlutterView flutterView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FlutterMain.ensureInitializationComplete(getApplicationContext(), null); setContentView(R.layout.hello_services_layout); flutterView = (FlutterView) findViewById(R.id.flutter_view); flutterView.runFromBundle(FlutterMain.findAppBundlePath(getApplicationContext()), null); final Context context = this; flutterView.addOnMessageListener("initDatabase", new FlutterView.OnMessageListener() { @Override public String onMessage(FlutterView flutterView, String s) { sqLiteHelper = new RawSQLiteHelper(context, flutterView, s, 1); return ""; } }); } - Update your ios
Podfileto include FMDB:
target 'Runner' do
Pods for Runner
pod 'FMDB'
end
5. run `pod install --no-repo-update` to install the *FMBD* pod 6. Copy the following files to your ios sub-project (should be called Runner):
DatabaseInitializer.h
DatabaseInitializer.m
SQLiteProvider.h
SQLiteProvider.m
7. Update `AppDelegate.m` to look something like this:
#import <Flutter/Flutter.h>
#include "AppDelegate.h"
#import "SQLiteProvider.h"
#import "DatabaseInitializer.h"
@implementation AppDelegate { DatabaseInitializer* _databaseInitializer; }
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FlutterDartProject* project = [[FlutterDartProject alloc] initFromDefaultSourceForConfiguration]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; FlutterViewController* flutterController = [[FlutterViewController alloc] initWithProject:project nibName:nil bundle:nil]; _databaseInitializer = [[DatabaseInitializer alloc] init]; [_databaseInitializer setFlutterController:flutterController]; [flutterController addMessageListener: _databaseInitializer];
self.window.rootViewController = flutterController; [self.window makeKeyAndVisible];
return YES; }
- Copy
SQLiteDatabase.dartto your flutter project (lib folder) - Now you can extend
SQLiteDatabaseto create your own repository, similar to how you extendandroid.database.sqlite.SQLiteDatabasein native android. See exampleExampleRepositoryfor an example.createDatabaseandupdateDatabaseact likeandroid.database.sqlite.SQLiteDatabaseas well.
