'Saving information to dart flutter database and pulling information to listTile
I have an application. In the application the user will save data. When you log into a particular page, the record of logging into that page will be saved in the database.
My problem is this: I examined the sqflite database structure, but I could not understand it. It's a strange building. What I need to do is to save data in only 1 column and pull them and put them in listTile.
But as I said, I couldn't do what I wanted because I couldn't understand the sqflite structure.
How can I do it? How do I use sqflite?
Solution 1:[1]
The sqflite library provides the sqlite database to flutter. Your question leads me to assume that you first need to read a bit more about what it is and what is used for.
Once you are familiar with the fundamentals you will be able to grasp the usage of the library fairly easily.
For your application though, I would suggest going for simpler options. You might find a key-value store like shared_preferences, to be easier to grasp and get started with. Just put the data as a JSON list in the store and retrieve it for display when building the ListView.
EDIT:
Use the following as a starting point and take it further as per your requirement:
import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
Database? db;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
db = await openDatabase(
'my_db.db',
version: 1,
onCreate: (Database db, int version) async {
await db.execute('CREATE TABLE Test (id INTEGER PRIMARY KEY, name TEXT)');
},
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Map<String, Object?>>? _records;
bool _loading = false;
int nextRecordId = 1;
@override
void initState() {
super.initState();
getRecords();
}
void getRecords() {
setState(() {
_loading = true;
});
db?.query('Test').then((value) {
setState(() {
_records = value;
_loading = false;
});
});
}
void _insertRandomRecord() {
db
?.insert(
'Test',
{
'id': '$nextRecordId',
'name': 'Random record $nextRecordId',
},
conflictAlgorithm: ConflictAlgorithm.replace)
.then((value) {
nextRecordId++;
getRecords();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _loading
? const Center(
child: CircularProgressIndicator.adaptive(),
)
: ListView.builder(
itemBuilder: (context, index) {
final record = _records![index];
return ListTile(
title: Text(record['name'] as String),
);
},
itemCount: _records?.length ?? 0,
),
floatingActionButton: FloatingActionButton(
onPressed: _insertRandomRecord,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
