skip to Main Content

I am not good in flutter but learning it . I am working with SQLite and want to fetch single record and want to map it to model class then want to show it in text widget.

Need help and thanks in advance.

Regards

2

Answers


  1. Future<User> fetchUser(String email) async {
        final db = await instance.database;
    
        final map = await db.rawQuery(
          "SELECT * FROM $tableName WHERE email = ?",[email]
        );
    
        if (map.isNotEmpty) {
          return User.fromJson(map.first);
        } else {
          throw Exception("User: $email not found");
        }
      }
    
    Login or Signup to reply.
  2.   import 'package:path/path.dart';
      import 'package:path_provider/path_provider.dart';
      import 'package:sqflite/sqflite.dart';
    
    
    
    
      Database? _db;
       static const createNotesTable = ''' 
       CREATE TABLE IF NOT EXISTS "notes" (
       "id" INTEGER NOT NULL,
       "userid" INTEGER NOT NULL,
       "text"   TEXT NOT NULL,
       PRIMARY KEY("id" AUTOINCREMENT)
       );''';
      const dbName = 'notes.db';
      const noteTable = 'note';
      ///Create Table
      Future<void>open()
       {
         final docsPath = await getApplicationDocumentsDirectory();
         final dbPath = join(docsPath.path, dbName);
         final db = await openDatabase(dbPath);
         _db = db;
    
         await db.execute(createNotesTable);
    
       }
    
      ///  get data using id 
      ///  DataBaseNotes is My Notes Model Class
      Future<DataBaseNotes> getNote({required int id}) async
       {
    
          final notes = await db.query(
          noteTable,
          limit: 1,
          where: 'id= ?',
          whereArgs: [id],
          );
        return note;
    
       }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search