text stringlengths 1 474 |
|---|
setState(() { |
_counter = value; |
}); |
}); |
} |
Future<File> _incrementCounter() { |
setState(() { |
_counter++; |
}); |
// Write the variable as a string to the file. |
return widget.storage.writeCounter(_counter); |
} |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
appBar: AppBar( |
title: const Text('Reading and Writing Files'), |
), |
body: Center( |
child: Text( |
'Button tapped $_counter time${_counter == 1 ? '' : 's'}.', |
), |
), |
floatingActionButton: FloatingActionButton( |
onPressed: _incrementCounter, |
tooltip: 'Increment', |
child: const Icon(Icons.add), |
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start>Persist data with SQLite |
info Note |
This guide uses the sqflite package. |
This package only supports apps that run on |
macOS, iOS, or Android.If you are writing an app that needs to persist and query large amounts of data on |
the local device, consider using a database instead of a local file or |
key-value store. In general, databases provide faster inserts, updates, |
and queries compared to other local persistence solutions.Flutter apps can make use of the SQLite databases via the |
sqflite plugin available on pub.dev. |
This recipe demonstrates the basics of using sqflite |
to insert, read, update, and remove data about various Dogs.If you are new to SQLite and SQL statements, review the |
SQLite Tutorial to learn the basics before |
completing this recipe.This recipe uses the following steps:<topic_end> |
<topic_start> |
1. Add the dependencies |
To work with SQLite databases, import the sqflite and |
path packages.To add the packages as a dependency, |
run flutter pub add:Make sure to import the packages in the file you’ll be working in. |
<code_start>import 'dart:async'; |
import 'package:flutter/widgets.dart'; |
import 'package:path/path.dart'; |
import 'package:sqflite/sqflite.dart';<code_end> |
<topic_end> |
<topic_start> |
2. Define the Dog data model |
Before creating the table to store information on Dogs, take a few moments to |
define the data that needs to be stored. For this example, define a Dog class |
that contains three pieces of data: |
A unique id, the name, and the age of each dog. |
<code_start>class Dog { |
final int id; |
final String name; |
final int age; |
const Dog({ |
required this.id, |
required this.name, |
required this.age, |
}); |
}<code_end> |
<topic_end> |
<topic_start> |
3. Open the database |
Before reading and writing data to the database, open a connection |
to the database. This involves two steps:info Note |
In order to use the keyword await, the code must be placed |
inside an async function. You should place all the following |
table functions inside void main() async {}. |
<code_start>// Avoid errors caused by flutter upgrade. |
// Importing 'package:flutter/widgets.dart' is required. |
WidgetsFlutterBinding.ensureInitialized(); |
// Open the database and store the reference. |
final database = openDatabase( |
// Set the path to the database. Note: Using the `join` function from the |
// `path` package is best practice to ensure the path is correctly |
// constructed for each platform. |
join(await getDatabasesPath(), 'doggie_database.db'), |
);<code_end> |
<topic_end> |
<topic_start> |
4. Create the dogs table |
Next, create a table to store information about various Dogs. |
For this example, create a table called dogs that defines the data |
that can be stored. Each Dog contains an id, name, and age. |
Therefore, these are represented as three columns in the dogs table.For more information about the available Datatypes that can be stored in a |
SQLite database, see the official SQLite Datatypes documentation. |
<code_start>final database = openDatabase( |
// Set the path to the database. Note: Using the `join` function from the |
// `path` package is best practice to ensure the path is correctly |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.