I created a mixture of two example codes, one from https://dartpad.dev/?id=e7076b40fb17a0fa899f9f7a154a02e8 and one from https://docs.flutter.dev/cookbook/persistence/reading-writing-files. I want to save in file the words what was liked by the user. For this I've created a new menu option.
This is my code:
`// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => MyAppState(),
child: MaterialApp(
title: 'Namer App',
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blueGrey),
),
home: MyHomePage(),
),
);
}
}
class Storage {
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/counter.txt');
}
Future<List> readWordList() async {
try {
final file = await _localFile;
// Read the file
// final contents = await file.readAsString();
//
List<String> lines = await file.readAsLines();
//
/*for (String line in lines) {
print(line);
}*/
return lines;
} catch (e) {
// If encountering an error, return 0
List<String> lines = [];
lines[0] ="ERR: Empty file!";
return lines;
}
}
Future<File> writeWord(String word) async {
final file = await _localFile;
// Write the file
return file.writeAsString('$word\n');
}
}
class MyAppState extends ChangeNotifier {
var current = WordPair.random();
var history = <WordPair>[];
GlobalKey? historyListKey;
void getNext() {
history.insert(0, current);
var animatedList = historyListKey?.currentState as AnimatedListState?;
animatedList?.insertItem(0);
current = WordPair.random();
notifyListeners();
}
var favorites = <WordPair>[];
void toggleFavorite([WordPair? pair]) {
pair = pair ?? current;
if (favorites.contains(pair)) {
favorites.remove(pair);
} else {
favorites.add(pair);
}
notifyListeners();
}
void removeFavorite(WordPair pair) {
favorites.remove(pair);
notifyListeners();
}
}
class MyHomePage extends StatefulWidget {
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var selectedIndex = 0;
@override
Widget build(BuildContext context) {
var colorScheme = Theme.of(context).colorScheme;
Widget page;
switch (selectedIndex) {
case 0:
page = GeneratorPage();
break;
case 1:
page = FavoritesPage();
break;
case 2:
page = SavingPage();
break;
default:
throw UnimplementedError('no widget for $selectedIndex');
}
// The container for the current page, with its background color
// and subtle switching animation.
var mainArea = ColoredBox(
color: colorScheme.surfaceVariant,
child: AnimatedSwitcher(
duration: Duration(milliseconds: 200),
child: page,
),
);
return Scaffold(
body: LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth < 450) {
// Use a more mobile-friendly layout with BottomNavigationBar
// on narrow screens.
return Column(
children: [
Expanded(child: mainArea),
SafeArea(
child: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
label: 'Favorites',
),
BottomNavigationBarItem(
icon: Icon(Icons.save),
label: 'Save',
),
],
currentIndex: selectedIndex,
onTap: (value) {
setState(() {
selectedIndex = value;
});
},
),
)
],
);
} else {
return Row(
children: [
SafeArea(
child: NavigationRail(
extended: constraints.maxWidth >= 600,
destinations: [
NavigationRailDestination(
icon: Icon(Icons.home),
label: Text('Home'),
),
NavigationRailDestination(
icon: Icon(Icons.favorite),
label: Text('Favorites'),
),
NavigationRailDestination(
icon: Icon(Icons.save),
label: Text('Save'),
),
],
selectedIndex: selectedIndex,
onDestinationSelected: (value) {
setState(() {
selectedIndex = value;
});
},
),
),
Expanded(child: mainArea),
],
);
}
},
),
);
}
}
class GeneratorPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var appState = context.watch<MyAppState>();
var pair = appState.current;
IconData icon;
if (appState.favorites.contains(pair)) {
icon = Icons.favorite;
} else {
icon = Icons.favorite_border;
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
flex: 3,
child: HistoryListView(),
),
SizedBox(height: 10),
BigCard(pair: pair),
SizedBox(height: 10),
Row(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton.icon(
onPressed: () {
appState.toggleFavorite();
},
icon: Icon(icon),
label: Text('Like'),
),
SizedBox(width: 10),
ElevatedButton(
onPressed: () {
appState.getNext();
},
child: Text('Next'),
),
],
),
Spacer(flex: 2),
],
),
);
}
}
class BigCard extends StatelessWidget {
const BigCard({
Key? key,
required this.pair,
}) : super(key: key);
final WordPair pair;
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var style = theme.textTheme.displayMedium!.copyWith(
color: theme.colorScheme.onPrimary,
);
return Card(
color: theme.colorScheme.primary,
child: Padding(
padding: const EdgeInsets.all(20),
child: AnimatedSize(
duration: Duration(milliseconds: 200),
// Make sure that the compound word wraps correctly when the window
// is too narrow.
child: MergeSemantics(
child: Wrap(
children: [
Text(
pair.first,
style: style.copyWith(fontWeight: FontWeight.w200),
),
Text(
pair.second,
style: style.copyWith(fontWeight: FontWeight.bold),
)
],
),
),
),
),
);
}
}
class FavoritesPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var appState = context.watch<MyAppState>();
if (appState.favorites.isEmpty) {
return Center(
child: Text('No favorites yet.'),
);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(30),
child: Text('You have '
'${appState.favorites.length} favorites:'),
),
Expanded(
// Make better use of wide windows with a grid.
child: GridView(
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 400,
childAspectRatio: 400 / 80,
),
children: [
for (var pair in appState.favorites)
ListTile(
leading: IconButton(
icon: Icon(Icons.delete_outline, semanticLabel: 'Delete'),
color: theme.colorScheme.primary,
onPressed: () {
appState.removeFavorite(pair);
},
),
title: Text(
pair.asLowerCase,
semanticsLabel: pair.asPascalCase,
),
),
],
),
),
],
);
}
}
class SavingPage extends StatelessWidget{
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var appState = context.watch<MyAppState>();
final storageHandler = context.watch<Storage>();
if (appState.favorites.isEmpty) {
return Center(
child: Text('No favorites yet.'),
);
} else{
for(var word in appState.favorites){
storageHandler.writeWord(word.asPascalCase);
}
return Center(
child: Text('Favorites saved!'),
);
}
}
}
class HistoryListView extends StatefulWidget {
const HistoryListView({Key? key}) : super(key: key);
@override
State<HistoryListView> createState() => _HistoryListViewState();
}
class _HistoryListViewState extends State<HistoryListView> {
/// Needed so that [MyAppState] can tell [AnimatedList] below to animate
/// new items.
final _key = GlobalKey();
/// Used to "fade out" the history items at the top, to suggest continuation.
static const Gradient _maskingGradient = LinearGradient(
// This gradient goes from fully transparent to fully opaque black...
colors: [Colors.transparent, Colors.black],
// ... from the top (transparent) to half (0.5) of the way to the bottom.
stops: [0.0, 0.5],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
);
@override
Widget build(BuildContext context) {
final appState = context.watch<MyAppState>();
appState.historyListKey = _key;
return ShaderMask(
shaderCallback: (bounds) => _maskingGradient.createShader(bounds),
// This blend mode takes the opacity of the shader (i.e. our gradient)
// and applies it to the destination (i.e. our animated list).
blendMode: BlendMode.dstIn,
child: AnimatedList(
key: _key,
reverse: true,
padding: EdgeInsets.only(top: 100),
initialItemCount: appState.history.length,
itemBuilder: (context, index, animation) {
final pair = appState.history[index];
return SizeTransition(
sizeFactor: animation,
child: Center(
child: TextButton.icon(
onPressed: () {
appState.toggleFavorite(pair);
},
icon: appState.favorites.contains(pair)
? Icon(Icons.favorite, size: 12)
: SizedBox(),
label: Text(
pair.asLowerCase,
semanticsLabel: pair.asPascalCase,
),
),
),
);
},
),
);
}
}`
This is the Error what I got when I click to the save option:
`======== Exception caught by widgets library =======================================================
The following ProviderNotFoundException was thrown building SavingPage(dependencies: [_InheritedProviderScope<MyAppState?>, _InheritedTheme, _LocalizationsScope-[GlobalKey#c825f]]):
Error: Could not find the correct Provider<Storage> above this SavingPage Widget
This happens because you used a `BuildContext` that does not include the provider
of your choice. There are a few common scenarios:
- You added a new provider in your `main.dart` and performed a hot-reload.
To fix, perform a hot-restart.
- The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then
other routes will not be able to access that provider.
- You used a `BuildContext` that is an ancestor of the provider you are trying to read.
Make sure that SavingPage is under your MultiProvider/Provider<Storage>.
This usually happens when you are creating a provider and trying to read it immediately.
For example, instead of:
```
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// Will throw a ProviderNotFoundError, because `context` is associated
// to the widget that is the parent of `Provider<Example>`
child: Text(context.watch<Example>().toString()),
);
}
```
consider using `builder` like so:
```
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// we use `builder` to obtain a new `BuildContext` that has access to the provider
builder: (context, child) {
// No longer throws
return Text(context.watch<Example>().toString());
}
);
}
```
If none of these solutions work, consider asking for help on StackOverflow:
https://stackoverflow.com/questions/tagged/flutter`
I'm newbie in Flutter, please give me some ideas how can I do this very simple thing?