Advertisement
sourav8256

Untitled

Aug 11th, 2023
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. import 'package:csv/csv.dart';
  6. import 'dart:io';
  7.  
  8. class CsvDatabaseManager {
  9. final String _csvFileName = 'data.csv';
  10.  
  11. Future<void> insertRecord(List<String> record) async {
  12. File file = await _getCsvFile();
  13. List<List<dynamic>> rows = [record];
  14. String csv = const ListToCsvConverter().convert(rows);
  15. await file.writeAsString(csv, mode: FileMode.append);
  16. }
  17.  
  18. Future<List<List<dynamic>>> getAllRecords() async {
  19. File file = await _getCsvFile();
  20. String csvString = await file.readAsString();
  21. List<List<dynamic>> csvTable = const CsvToListConverter().convert(csvString);
  22. return csvTable;
  23. }
  24.  
  25. Future<void> updateRecord(int rowIndex, List<String> newRecord) async {
  26. List<List<dynamic>> records = await getAllRecords();
  27. if (rowIndex >= 0 && rowIndex < records.length) {
  28. records[rowIndex] = newRecord;
  29. await _saveRecords(records);
  30. }
  31. }
  32.  
  33. Future<void> deleteRecord(int rowIndex) async {
  34. List<List<dynamic>> records = await getAllRecords();
  35. if (rowIndex >= 0 && rowIndex < records.length) {
  36. records.removeAt(rowIndex);
  37. await _saveRecords(records);
  38. }
  39. }
  40.  
  41. Future<void> _saveRecords(List<List<dynamic>> records) async {
  42. File file = await _getCsvFile();
  43. String csv = const ListToCsvConverter().convert(records);
  44. await file.writeAsString(csv);
  45. }
  46.  
  47. Future<File> _getCsvFile() async {
  48. Directory directory = await Directory.systemTemp.createTemp();
  49. File file = File('${directory.path}/$_csvFileName');
  50. if (!(await file.exists())) {
  51. await file.create();
  52. }
  53. return file;
  54. }
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. =======================================================================================================================
  63.  
  64.  
  65.  
  66.  
  67.  
  68. import 'package:flutter/material.dart';
  69.  
  70. void main() {
  71. runApp(MyApp());
  72. }
  73.  
  74. class MyApp extends StatelessWidget {
  75. @override
  76. Widget build(BuildContext context) {
  77. return MaterialApp(
  78. title: 'CSV Database Manager',
  79. theme: ThemeData(
  80. primarySwatch: Colors.blue,
  81. ),
  82. home: MyHomePage(),
  83. );
  84. }
  85. }
  86.  
  87. class MyHomePage extends StatefulWidget {
  88. @override
  89. _MyHomePageState createState() => _MyHomePageState();
  90. }
  91.  
  92. class _MyHomePageState extends State<MyHomePage> {
  93. CsvDatabaseManager _databaseManager = CsvDatabaseManager();
  94.  
  95. void _insertRecord() async {
  96. List<String> record = ['John Doe', 'johndoe@example.com', '30'];
  97. await _databaseManager.insertRecord(record);
  98. ScaffoldMessenger.of(context).showSnackBar(
  99. SnackBar(
  100. content: Text('Record inserted'),
  101. ),
  102. );
  103. }
  104.  
  105. Future<void> _displayRecords() async {
  106. List<List<dynamic>> records = await _databaseManager.getAllRecords();
  107. showDialog(
  108. context: context,
  109. builder: (context) {
  110. return AlertDialog(
  111. title: Text('CSV Records'),
  112. content: SingleChildScrollView(
  113. child: Column(
  114. children: records.map((record) {
  115. return ListTile(
  116. title: Text(record[0]),
  117. subtitle: Text(record[1]),
  118. );
  119. }).toList(),
  120. ),
  121. ),
  122. actions: [
  123. TextButton(
  124. onPressed: () {
  125. Navigator.pop(context);
  126. },
  127. child: Text('Close'),
  128. ),
  129. ],
  130. );
  131. },
  132. );
  133. }
  134.  
  135. @override
  136. Widget build(BuildContext context) {
  137. return Scaffold(
  138. appBar: AppBar(
  139. title: Text('CSV Database Manager'),
  140. ),
  141. body: Center(
  142. child: Column(
  143. mainAxisAlignment: MainAxisAlignment.center,
  144. children: [
  145. ElevatedButton(
  146. onPressed: _insertRecord,
  147. child: Text('Insert Record'),
  148. ),
  149. SizedBox(height: 16.0),
  150. ElevatedButton(
  151. onPressed: _displayRecords,
  152. child: Text('Display Records'),
  153. ),
  154. ],
  155. ),
  156. ),
  157. );
  158. }
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement