joaopaulofcc

Untitled

Nov 11th, 2020
579
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'dart:async';
  2.  
  3. import 'package:flutter/widgets.dart';
  4.  
  5. import 'package:path/path.dart';
  6. import 'package:sqflite/sqflite.dart';
  7.  
  8. class Dog {
  9.   final int id;
  10.   final String name;
  11.   final int age;
  12.  
  13.   Dog({this.id, this.name, this.age});
  14.  
  15.   Map<String, dynamic> toMap() {
  16.     return {
  17.       'id': id,
  18.       'name': name,
  19.       'age': age,
  20.     };
  21.   }
  22.  
  23.   @override
  24.   String toString() {
  25.     return 'Dados do cachorro $id - Nome: $name, Idade: $age';
  26.   }
  27. }
  28.  
  29. void main() async {
  30.   WidgetsFlutterBinding.ensureInitialized();
  31.  
  32.   final Future<Database> database = openDatabase(
  33.     join(await getDatabasesPath(), 'bancoAppCachorro.db'),
  34.     onCreate: (db, version) {
  35.       return db.execute(
  36.           "CREATE TABLE dogs(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)");
  37.     },
  38.     version: 1,
  39.   );
  40.  
  41.   Future<void> insertDog(Dog dog) async {
  42.     final Database db = await database;
  43.  
  44.     await db.insert('dogs', dog.toMap(),
  45.         conflictAlgorithm: ConflictAlgorithm.replace);
  46.   }
  47.  
  48.   Future<Dog> getDog(int id) async {
  49.     final Database db = await database;
  50.  
  51.     final List<Map<String, dynamic>> maps =
  52.         await db.query('dogs', where: "id = ?", whereArgs: [id], limit: 1);
  53.  
  54.     if (maps.length > 0) {
  55.       return Dog(id: maps[0]['id'], name: maps[0]['name'], age: maps[0]['age']);
  56.     } else {
  57.       return null;
  58.     }
  59.   }
  60.  
  61.   Future<List<Dog>> getDogs() async {
  62.     final Database db = await database;
  63.  
  64.     final List<Map<String, dynamic>> maps = await db.query('dogs');
  65.  
  66.     return List.generate(maps.length, (i) {
  67.       return Dog(id: maps[i]['id'], name: maps[i]['name'], age: maps[i]['age']);
  68.     });
  69.   }
  70.  
  71.   Future<void> updateDog(Dog dog) async {
  72.     final Database db = await database;
  73.  
  74.     db.update(
  75.       'dogs',
  76.       dog.toMap(),
  77.       where: "id = ?",
  78.       whereArgs: [dog.id],
  79.     );
  80.   }
  81.  
  82.   Future<void> deleteDog(int id) async {
  83.     final Database db = await database;
  84.  
  85.     await db.delete('dogs', where: "id = ?", whereArgs: [id]);
  86.   }
  87.  
  88.   Dog cachorro1 = Dog(id: 1, name: 'Rex', age: 35);
  89.   await insertDog(cachorro1);
  90.   print(await getDogs());
  91.  
  92.   Dog cachorro2 = Dog(id: 2, name: 'Bretas', age: 2);
  93.   await insertDog(cachorro2);
  94.   print(await getDogs());
  95.  
  96.   cachorro2 =
  97.       Dog(id: cachorro2.id, name: 'Bretas o Dog', age: cachorro2.age + 1);
  98.   await updateDog(cachorro2);
  99.   print(await getDogs());
  100.  
  101.   await deleteDog(2);
  102.  
  103.   print(await getDog(1));
  104. }
RAW Paste Data