Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:csv/csv.dart';
- import 'dart:io';
- class CsvDatabaseManager {
- final String _csvFileName = 'data.csv';
- Future<void> insertRecord(List<String> record) async {
- File file = await _getCsvFile();
- List<List<dynamic>> rows = [record];
- String csv = const ListToCsvConverter().convert(rows);
- await file.writeAsString(csv, mode: FileMode.append);
- }
- Future<List<List<dynamic>>> getAllRecords() async {
- File file = await _getCsvFile();
- String csvString = await file.readAsString();
- List<List<dynamic>> csvTable = const CsvToListConverter().convert(csvString);
- return csvTable;
- }
- Future<void> updateRecord(int rowIndex, List<String> newRecord) async {
- List<List<dynamic>> records = await getAllRecords();
- if (rowIndex >= 0 && rowIndex < records.length) {
- records[rowIndex] = newRecord;
- await _saveRecords(records);
- }
- }
- Future<void> deleteRecord(int rowIndex) async {
- List<List<dynamic>> records = await getAllRecords();
- if (rowIndex >= 0 && rowIndex < records.length) {
- records.removeAt(rowIndex);
- await _saveRecords(records);
- }
- }
- Future<void> _saveRecords(List<List<dynamic>> records) async {
- File file = await _getCsvFile();
- String csv = const ListToCsvConverter().convert(records);
- await file.writeAsString(csv);
- }
- Future<File> _getCsvFile() async {
- Directory directory = await Directory.systemTemp.createTemp();
- File file = File('${directory.path}/$_csvFileName');
- if (!(await file.exists())) {
- await file.create();
- }
- return file;
- }
- }
- =======================================================================================================================
- import 'package:flutter/material.dart';
- void main() {
- runApp(MyApp());
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'CSV Database Manager',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: MyHomePage(),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- CsvDatabaseManager _databaseManager = CsvDatabaseManager();
- void _insertRecord() async {
- List<String> record = ['John Doe', 'johndoe@example.com', '30'];
- await _databaseManager.insertRecord(record);
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: Text('Record inserted'),
- ),
- );
- }
- Future<void> _displayRecords() async {
- List<List<dynamic>> records = await _databaseManager.getAllRecords();
- showDialog(
- context: context,
- builder: (context) {
- return AlertDialog(
- title: Text('CSV Records'),
- content: SingleChildScrollView(
- child: Column(
- children: records.map((record) {
- return ListTile(
- title: Text(record[0]),
- subtitle: Text(record[1]),
- );
- }).toList(),
- ),
- ),
- actions: [
- TextButton(
- onPressed: () {
- Navigator.pop(context);
- },
- child: Text('Close'),
- ),
- ],
- );
- },
- );
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('CSV Database Manager'),
- ),
- body: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- ElevatedButton(
- onPressed: _insertRecord,
- child: Text('Insert Record'),
- ),
- SizedBox(height: 16.0),
- ElevatedButton(
- onPressed: _displayRecords,
- child: Text('Display Records'),
- ),
- ],
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement