# Set Up Local Datastore
Follow the steps to set up sqlite and shared preference which are used to store data locally within the app
# Shared preference
- Create folder
lib/datastore - Add latest shared_preferences (opens new window) package to pubspec.yaml
- Click
Pub geton Android Studio - Download shared_pref folder from here (opens new window) and add it inside
lib/datastore/folder - Download configure folder from here (opens new window) and add it inside
lib/folder - Change parameters of AppInfo in
lib/configure/app_info.dart - Call
Initializer().initialize();inside main function afterWidgetsFlutterBinding.ensureInitialized();inmain.dart
void main() async {
/// Initialize firebase app
WidgetsFlutterBinding.ensureInitialized();
/// Initialize packages
await Initializer().initialize();
/// Orientation
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
runApp(const MyApp());
}
# Sqlite
- Add latest sqflite (opens new window) package to pubspec.yaml
- Add compatible path (opens new window) package to pubspec.yaml
- Add latest path_provider (opens new window) package to pubspec.yaml
- Click
Pub geton Android Studio - Download sqlite folder from here (opens new window) and add it inside
lib/datastore/folder - Download data_objects folder from here (opens new window) and add it inside
lib/datastore/folder - Add additional parameters to User class in
lib/datastore/data_objects/user.dartif needed, based on your app's requirements. Also add these parameters when you createUsertable inlib/datastore/sqlite/user.dart - Initialize
sqliteinlib/configure/initializer.dartas shown below
import '../datastore/sqlite/sqlite.dart';
import '../datastore/shared_pref/shared_pref.dart';
class Initializer {
initialize() async {
await initializeDatastore();
}
/// Initialize sqlite and shared preferences
initializeDatastore() async {
/// Initializes shared_preference
SharedPref().sharedPrefInit();
/// Initialize sq-lite
final db = SqliteDB();
await db.countTable();
}
}
- When you call
await db.countTable();in previous code, if a database is not present, new database is created. Now we have to add tables to database. Creating sqlite table should be done on Splash Screen by callingSqliteDB().createTables();
10 . You will be creating a splash screen in next step