Created
April 25, 2024 12:01
-
-
Save jeerovan/a0caea5cdbfab049b4818a77f0e32311 to your computer and use it in GitHub Desktop.
SQLite DB browser in Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:sqflite/sqflite.dart'; | |
import 'package:path/path.dart'; | |
class DatabasePage extends StatefulWidget { | |
@override | |
State<DatabasePage> createState() => _DatabasePageState(); | |
} | |
class _DatabasePageState extends State<DatabasePage> { | |
Database? _database; | |
List<String> _tables = []; | |
String? _selectedTable; | |
List<Map<String, dynamic>> _tableData = []; | |
@override | |
void initState() { | |
super.initState(); | |
_initDatabase(); | |
} | |
Future<void> _initDatabase() async { | |
_database = await openDatabase( | |
join(await getDatabasesPath(), 'your_database_name.db'), | |
); | |
List<Map<String, dynamic>> tables = | |
await _database!.rawQuery("SELECT name FROM sqlite_master WHERE type='table';"); | |
_tables = tables.map((table) => table['name'] as String).toList(); | |
setState(() {}); | |
} | |
Future<void> _loadTableData(String tableName) async { | |
List<Map<String, dynamic>> tableData = await _database!.query(tableName); | |
setState(() { | |
_selectedTable = tableName; | |
_tableData = tableData; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: _database == null | |
? Center(child: CircularProgressIndicator()) | |
: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Expanded( | |
child: _selectedTable == null | |
? Center(child: Text('Select a table to view its data')) | |
: SingleChildScrollView( | |
scrollDirection: Axis.vertical, | |
child: SingleChildScrollView( | |
scrollDirection: Axis.horizontal, | |
child: DataTable( | |
columns: _tableData.isNotEmpty | |
? _tableData.first.keys.map((String columnName) { | |
return DataColumn( | |
label: Text(columnName), | |
); | |
}).toList() | |
: [], | |
rows: _tableData.map((Map<String, dynamic> rowData) { | |
return DataRow( | |
cells: rowData.keys.map((String columnName) { | |
return DataCell( | |
Text(rowData[columnName].toString()), | |
); | |
}).toList(), | |
); | |
}).toList(), | |
), | |
), | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: DropdownButton<String>( | |
hint: Text('Select a table'), | |
value: _selectedTable, | |
onChanged: (String? tableName) { | |
if (tableName != null) { | |
_loadTableData(tableName); | |
} | |
}, | |
items: _tables.map((tableName) { | |
return DropdownMenuItem<String>( | |
value: tableName, | |
child: Text(tableName), | |
); | |
}).toList(), | |
), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment