29/11/2024

Tech Guru

Trusted Source Technology

Flutter Drift Database Implementation – AndroidCoding.in

Flutter Drift Database Implementation – AndroidCoding.in

Drift database :

Drift database is a reactive library used to store data locally built on top of sqlite database in your mobile apps.

 

 

pubspec.yaml :

Add the required libraries for the drift.

dependencies:
  
  drift: ^1.7.1
  sqlite3_flutter_libs: ^0.5.0
  path_provider: ^2.0.0
  path: ^1.8.0

 

dev_dependencies:

  drift_dev: ^1.7.0
  build_runner: ^2.1.11

 

data.dart :

import 'package:drift/drift.dart';

part 'data.g.dart';

class Products extends Table 

  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text()();
  TextColumn get description => text()();


abstract class ProductsView extends View
  Products get products;

  @override
  Query as() => select([
    products.title
  ]).from(products);


@DriftDatabase(tables:[
  Products
], views:[
  ProductsView
])

class Database extends _$Database 
  Database(QueryExecutor e): super(e);

  @override
  int get schemaVersion => 2;

 

data.g.dart :

This is the generated file for data.dart file.

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'data.dart';

// **************************************************************************
// MoorGenerator
// **************************************************************************

// ignore_for_file: type=lint
class Product extends DataClass implements Insertable<Product> 
  final int id;
  final String title;
  final String description;
  Product(required this.id, required this.title, required this.description);
  factory Product.fromData(Map<String, dynamic> data, String? prefix) 
    final effectivePrefix = prefix ?? '';
    return Product(
      id: const IntType()
          .mapFromDatabaseResponse(data['$effectivePrefixid'])!,
      title: const StringType()
          .mapFromDatabaseResponse(data['$effectivePrefixtitle'])!,
      description: const StringType()
          .mapFromDatabaseResponse(data['$effectivePrefixdescription'])!,
    );
  
  @override
  Map<String, Expression> toColumns(bool nullToAbsent) 
    final map = <String, Expression>;
    map['id'] = Variable<int>(id);
    map['title'] = Variable<String>(title);
    map['description'] = Variable<String>(description);
    return map;
  

  ProductsCompanion toCompanion(bool nullToAbsent) 
    return ProductsCompanion(
      id: Value(id),
      title: Value(title),
      description: Value(description),
    );
  

  factory Product.fromJson(Map<String, dynamic> json,
      ValueSerializer? serializer) 
    serializer ??= driftRuntimeOptions.defaultSerializer;
    return Product(
      id: serializer.fromJson<int>(json['id']),
      title: serializer.fromJson<String>(json['title']),
      description: serializer.fromJson<String>(json['description']),
    );
  
  @override
  Map<String, dynamic> toJson(ValueSerializer? serializer) 
    serializer ??= driftRuntimeOptions.defaultSerializer;
    return <String, dynamic>
      'id': serializer.toJson<int>(id),
      'title': serializer.toJson<String>(title),
      'description': serializer.toJson<String>(description),
    ;
  

  Product copyWith(int? id, String? title, String? description) => Product(
        id: id ?? this.id,
        title: title ?? this.title,
        description: description ?? this.description,
      );
  @override
  String toString() 
    return (StringBuffer('Product(')
          ..write('id: $id, ')
          ..write('title: $title, ')
          ..write('description: $description')
          ..write(')'))
        .toString();
  

  @override
  int get hashCode => Object.hash(id, title, description);
  @override
  bool operator ==(Object other) =>
      identical(this, other) 

class ProductsCompanion extends UpdateCompanion<Product> 
  final Value<int> id;
  final Value<String> title;
  final Value<String> description;
  const ProductsCompanion(
    this.id = const Value.absent(),
    this.title = const Value.absent(),
    this.description = const Value.absent(),
  );
  ProductsCompanion.insert(
    this.id = const Value.absent(),
    required String title,
    required String description,
  )  : title = Value(title),
        description = Value(description);
  static Insertable<Product> custom(
    Expression<int>? id,
    Expression<String>? title,
    Expression<String>? description,
  ) 
    return RawValuesInsertable(
      if (id != null) 'id': id,
      if (title != null) 'title': title,
      if (description != null) 'description': description,
    );
  

  ProductsCompanion copyWith(
      Value<int>? id, Value<String>? title, Value<String>? description) 
    return ProductsCompanion(
      id: id ?? this.id,
      title: title ?? this.title,
      description: description ?? this.description,
    );
  

  @override
  Map<String, Expression> toColumns(bool nullToAbsent) 
    final map = <String, Expression>;
    if (id.present) 
      map['id'] = Variable<int>(id.value);
    
    if (title.present) 
      map['title'] = Variable<String>(title.value);
    
    if (description.present) 
      map['description'] = Variable<String>(description.value);
    
    return map;
  

  @override
  String toString() 
    return (StringBuffer('ProductsCompanion(')
          ..write('id: $id, ')
          ..write('title: $title, ')
          ..write('description: $description')
          ..write(')'))
        .toString();
  


class $ProductsTable extends Products with TableInfo<$ProductsTable, Product> 
  @override
  final GeneratedDatabase attachedDatabase;
  final String? _alias;
  $ProductsTable(this.attachedDatabase, [this._alias]);
  final VerificationMeta _idMeta = const VerificationMeta('id');
  @override
  late final GeneratedColumn<int?> id = GeneratedColumn<int?>(
      'id', aliasedName, false,
      type: const IntType(),
      requiredDuringInsert: false,
      defaultConstraints: 'PRIMARY KEY AUTOINCREMENT');
  final VerificationMeta _titleMeta = const VerificationMeta('title');
  @override
  late final GeneratedColumn<String?> title = GeneratedColumn<String?>(
      'title', aliasedName, false,
      type: const StringType(), requiredDuringInsert: true);
  final VerificationMeta _descriptionMeta =
      const VerificationMeta('description');
  @override
  late final GeneratedColumn<String?> description = GeneratedColumn<String?>(
      'description', aliasedName, false,
      type: const StringType(), requiredDuringInsert: true);
  @override
  List<GeneratedColumn> get $columns => [id, title, description];
  @override
  String get aliasedName => _alias ?? 'products';
  @override
  String get actualTableName => 'products';
  @override
  VerificationContext validateIntegrity(Insertable<Product> instance,
      bool isInserting = false) 
    final context = VerificationContext();
    final data = instance.toColumns(true);
    if (data.containsKey('id')) 
      context.handle(_idMeta, id.isAcceptableOrUnknown(data['id']!, _idMeta));
    
    if (data.containsKey('title')) 
      context.handle(
          _titleMeta, title.isAcceptableOrUnknown(data['title']!, _titleMeta));
     else if (isInserting) 
      context.missing(_titleMeta);
    
    if (data.containsKey('description')) 
      context.handle(
          _descriptionMeta,
          description.isAcceptableOrUnknown(
              data['description']!, _descriptionMeta));
     else if (isInserting) 
      context.missing(_descriptionMeta);
    
    return context;
  

  @override
  Set<GeneratedColumn> get $primaryKey => id;
  @override
  Product map(Map<String, dynamic> data, String? tablePrefix) 
    return Product.fromData(data,
        prefix: tablePrefix != null ? '$tablePrefix.' : null);
  

  @override
  $ProductsTable createAlias(String alias) 
    return $ProductsTable(attachedDatabase, alias);
  


class ProductsViewData extends DataClass 
      (other is ProductsViewData && other.title == this.title);


class $ProductsViewView extends ViewInfo<$ProductsViewView, ProductsViewData>
    implements HasResultSet 
  final String? _alias;
  @override
  final _$Database attachedDatabase;
  $ProductsViewView(this.attachedDatabase, [this._alias]);
  $ProductsTable get products => attachedDatabase.products.createAlias('t0');
  @override
  List<GeneratedColumn> get $columns => [products.title];
  @override
  String get aliasedName => _alias ?? entityName;
  @override
  String get entityName => 'products_view';
  @override
  String? get createViewStmt => null;
  @override
  $ProductsViewView get asDslTable => this;
  @override
  ProductsViewData map(Map<String, dynamic> data, String? tablePrefix) 
    return ProductsViewData.fromData(data,
        prefix: tablePrefix != null ? '$tablePrefix.' : null);
  

  late final GeneratedColumn<String?> title = GeneratedColumn<String?>(
      'title', aliasedName, false,
      type: const StringType());
  @override
  $ProductsViewView createAlias(String alias) 
    return $ProductsViewView(attachedDatabase, alias);
  

  @override
  Query? get query =>
      (attachedDatabase.selectOnly(products, includeJoinedTableColumns: false)
        ..addColumns($columns));
  @override
  Set<String> get readTables => const 'products';


abstract class _$Database extends GeneratedDatabase 
  _$Database(QueryExecutor e) : super(SqlTypeSystem.defaultInstance, e);
  late final $ProductsTable products = $ProductsTable(this);
  late final $ProductsViewView productsView = $ProductsViewView(this);
  @override
  Iterable<TableInfo> get allTables => allSchemaEntities.whereType<TableInfo>();
  @override
  List<DatabaseSchemaEntity> get allSchemaEntities => [products, productsView];

 

main.dart :

Providing the full code for implementation for drift database.

import 'package:drift/native.dart';
import 'data.dart';

Future<void> main() async 

  final db = Database(NativeDatabase.memory());

  await db.into(db.products).insert(ProductsCompanion.insert(title: "flutter drift",
      description: "Drift database"));
  await db.into(db.products).insert(ProductsCompanion.insert(title: "tutorial on drift",
      description: "Drift database"));
  (await db.select(db.products).get()).forEach(print);

 

 

output :