You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I'm new of state manager, I'm just started to learning.
I'm using drift to manage database data, I followed a tutorial online that use provider, but since is complicated for me, I started to use getX on other part of the application, now I would like to remove provider parts and use only getX;
So, Drift wiki say:
Using the database
If you've created a MyDatabase class by following the getting started guide, you still need to somehow obtain an instance of it. It's recommended to only have one (singleton) instance of your database, so you could store that instance in a global variable:
If you're using the GetX package, you can add it as a service that manages the database instance:.
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(path.join(dbFolder.path,'TEST_db.db'));
// 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);
});
}
@DriftDatabase(tables: [ITEM, ORDER],daos: [ITEMDao,ORDERDao])
class AppDb extends _$AppDb{
AppDb() : super(_openConnection());
@override
int get schemaVersion =>1;
}
and inside the screens where I need to manage data:
for example to build a list:
body: StreamBuilder<List<ORDERData>>(
stream:Provider.of<AppDb>(context, listen: true).ORDERDao.getORDERs(),
builder: (context, snapshot) {
final List<ORDERData>? order = snapshot.data;
if (snapshot.connectionState != ConnectionState.active) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.hasError) {
return Center(
child: Text(snapshot.error.toString()),
);
}
if (order != null) {
return ListView.builder(.........
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello, I'm new of state manager, I'm just started to learning.
I'm using drift to manage database data, I followed a tutorial online that use provider, but since is complicated for me, I started to use getX on other part of the application, now I would like to remove provider parts and use only getX;
So, Drift wiki say:
Using the database
If you've created a MyDatabase class by following the getting started guide, you still need to somehow obtain an instance of it. It's recommended to only have one (singleton) instance of your database, so you could store that instance in a global variable:
If you're using the GetX package, you can add it as a service that manages the database instance:.
here my actual code:
and inside the screens where I need to manage data:
for example to build a list:
or to to add an Order:
void AddORDER(){
and in the main.dart:
Can you help me to convert it to use getX?
Beta Was this translation helpful? Give feedback.
All reactions