text stringlengths 1 372 |
|---|
future<int> readCounter() async { |
try { |
final file = await _localFile; |
// read the file |
final contents = await file.readAsString(); |
return int.parse(contents); |
} catch (e) { |
// if encountering an error, return 0 |
return 0; |
} |
} |
Future<File> writeCounter(int counter) async { |
final file = await _localFile; |
// write the file |
return file.writeAsString('$counter'); |
} |
} |
class FlutterDemo extends StatefulWidget { |
const FlutterDemo({super.key, required this.storage}); |
final CounterStorage storage; |
@override |
State<FlutterDemo> createState() => _FlutterDemoState(); |
} |
class _FlutterDemoState extends State<FlutterDemo> { |
int _counter = 0; |
@override |
void initState() { |
super.initState(); |
widget.storage.readCounter().then((value) { |
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: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.