SHARE
TWEET

VS Code User Snippets - Dart For Flutter

iansaimima Jan 23rd, 2020 (edited) 116 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.     "Create Stateful Widget" : {
  3.         "prefix": "statefulWidget",
  4.         "body": [
  5.             "class ${1:className}Page extends StatefulWidget{",
  6.             "\t@override",
  7.             "\t_${1:className}PageState createState() => _${1:className}PageState();",
  8.             "}",
  9.             "\n",
  10.             "class _${1:className}PageState extends State<${1:className}Page>{",
  11.             "\t@override",
  12.             "\tWidget build(BuildContext context){",
  13.             "\t\treturn Scaffold(",
  14.             "\t\t\tappBar: AppBar(",
  15.             "\t\t\t\ttitle: Text('${1:className}')",
  16.             "\t\t\t),",
  17.             "\t\t\tbody: Center(",
  18.             "\t\t\t\tchild: Text('${1:className}')",
  19.             "\t\t\t)",
  20.             "\t\t);",
  21.             "\t}",
  22.             "}",
  23.         ],
  24.         "description": "Class Statefull set"
  25.     },
  26.  
  27.     "SQFLite Database Helper" : {
  28.         "prefix": "sqfliteDatabaseHelper",
  29.         "body": [
  30.             "import 'package:sqflite/sqflite.dart';",
  31.             "import 'dart:async';",
  32.             "import 'dart:io';",
  33.             "import 'package:path_provider/path_provider.dart';",
  34.             "",
  35.             "class DatabaseHelper {",
  36.             "",
  37.             "\tstatic DatabaseHelper _databaseHelper;",
  38.             "\tstatic Database _database;",
  39.             "",
  40.             "\tDatabaseHelper._createInstance();",
  41.             "",
  42.             "\tfactory DatabaseHelper(){",
  43.             "",
  44.             "\t\tif(_databaseHelper == null){",
  45.             "\t\t\t_databaseHelper = DatabaseHelper._createInstance();",
  46.             "\t\t}",
  47.             "",
  48.             "\t\treturn _databaseHelper;",
  49.             "\t}",
  50.             "",
  51.             "\tFuture<Database> get database async{",
  52.             "",
  53.             "\t\tif(_database == null){",
  54.             "\t\t\t_database = await initializeDatabase();",
  55.             "\t\t}",
  56.             "",
  57.             "\t\treturn _database;",
  58.             "\t}",
  59.             "",
  60.             "\tFuture<Database> initializeDatabase() async{",
  61.             "\t\tDirectory directory = await getApplicationDocumentsDirectory();",
  62.             "\t\tString path = directory.path + \"${1:database_name}\";",
  63.             "",
  64.             "\t\tvar mDatabase = await openDatabase(path, version: 1, onCreate: _createDb);",
  65.             "\t\treturn mDatabase;",
  66.             "\t}",
  67.             "",
  68.             "\tvoid _createDb(Database db, int newVersion) async {",
  69.             "\t\tawait db.execute(\"${2:query for create table}\");",
  70.             "\t}",
  71.             "}",
  72.         ]
  73.     },
  74.  
  75.     "SQFLite Get Map List" : {
  76.         "prefix": "sqfliteGetMapList",
  77.         "body": [
  78.             "// Fetch $1MapList",
  79.             "Future<List<Map<String, dynamic>>> $1GetMapList(Map<String, dynamic> filters) async {",
  80.             "\tDatabase db = await this.database;",
  81.             "\tvar result = List<Map<String, dynamic>>();",
  82.             "",
  83.             "\tList whereStringList = List();",
  84.             "\tif(filters.length > 0) { ",
  85.             "\t\tfilters.forEach((k, v){",
  86.             "\t\t\tif(v != null) {",
  87.             "\t\t\t\tvar number = int.parse(v);",
  88.             "\t\t\t\tif(number.isEven || number.isOdd) {",
  89.             "\t\t\t\t\twhereStringList.add(\" \\$k = \\$v\");",
  90.             "\t\t\t\t}else{",
  91.             "\t\t\t\t\twhereStringList.add(\" \\$k = '\\$v'\");",
  92.             "\t\t\t\t}",
  93.             "\t\t\t}",
  94.             "\t\t});",
  95.             "",
  96.             "\t\tString whereString = \"\";",
  97.             "\t\tif (whereStringList.length > 0) {",
  98.             "\t\t\twhereString = whereStringList.join(\" and \");",
  99.             "\t\t}",
  100.             "\n",
  101.             "\t\tif(whereString.isNotEmpty) {",
  102.             "\t\t\tresult = await db.query(\"$1\", where: whereString, orderBy: '$2 asc');",
  103.             "\t\t}else{",
  104.             "\t\t\tresult = await db.query(\"$1\", orderBy: '$2 asc');",
  105.             "\t\t}",
  106.             "",
  107.             "\t}",
  108.             "\treturn result;",
  109.             "}",
  110.         ]
  111.     },
  112.  
  113.     "SQFLite Replace" : {
  114.         "prefix": "sqfliteReplace",
  115.         "body": [
  116.            
  117.             "// Replace Operation : Replace $1 Object to Database",
  118.             "Future<int> $2Replace($1 $2) async {",
  119.             "\tDatabase db = await this.database;",
  120.             "",
  121.             "\tvar result = 0;",
  122.             "\tif($2.$3 != null && $2.$3 > 0){",
  123.             "\t\tresult = await db.update(\"$2\", $2.toMap(), where: \"$3 = \\${$2.$3}\");",
  124.             "\t}else{",
  125.             "\t\tresult = await db.insert(\"$2\", $2.toMap());",
  126.             "\t}",
  127.             "\treturn result;",
  128.             "}"
  129.         ]  
  130.     },
  131.  
  132.     "SQFLite Update" : {
  133.         "prefix": "sqfliteUpdate",
  134.         "body": [
  135.             "// Update operation : update $1 to database",
  136.             "Future<int> $2Update($1 $2) async{",
  137.             "\tDatabase db = await this.database;",
  138.             "",
  139.             "\tvar result = await db.update(\"$2\", $2.toMap(), where: '$3 = ?', whereArgs: [$2.$3]);",
  140.             "\treturn result;",
  141.             "}"
  142.         ]      
  143.     },
  144.  
  145.     "SQFLite Delete" : {
  146.         "prefix": "sqfliteDelete",
  147.         "body": [
  148.            
  149.             "// Delete operation : delete $1 object from Database",
  150.             "Future<int> $1Delete(int id) async {",
  151.             "\tDatabase db = await this.database;",
  152.             "",
  153.             "\tvar result = await db.rawDelete('DELETE FROM $1 WHERE $2 = \\$$2}');",
  154.             "\treturn result;",
  155.             "}"
  156.         ]        
  157.     }
  158. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top