簡體   English   中英

保存信息到dart flutter數據庫拉取信息到listTile

[英]Saving information to dart flutter database and pulling information to listTile

我有一個應用程序。 在應用程序中,用戶將保存數據。 當您登錄到特定頁面時,登錄該頁面的記錄將保存在數據庫中。

我的問題是:我檢查了sqflite數據庫結構,但無法理解。 這是一座奇怪的建築。 我需要做的是僅將數據保存在 1 列中,然后將它們拉出並放入listTile中。

但是就像我說的,我不能如願以償,因為我看不懂sqflite的結構。

我該怎么做? 如何使用sqflite

sqflite庫提供sqlite數據庫到 flutter。你的問題讓我假設你首先需要閱讀更多關於它是什么以及它的用途。

熟悉基礎知識后,您將能夠相當輕松地掌握庫的用法。

不過,對於您的應用程序,我建議您選擇更簡單的選項。 您可能會發現像shared_preferences這樣的鍵值存儲更容易掌握和開始使用。 只需將數據作為 JSON 列表放入存儲中,並在構建 ListView 時檢索它以供顯示。

編輯:

使用以下內容作為起點,並根據您的要求進一步發展:

  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),
        ),
      );
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM