I am learning Flutter currently and was making a personal finance app. I have the option to bookmark my guides and then view them on the bookmark tab. Right now, I am using a list to simply store names of guides and display them as list tiles.
The issue I am having is that whenever the bookmark list is updated WHILE the app is running, the Bookmarks page loads the right info but then when I close and restart the app, it goes back to it's initial state of being empty. How can I fix it so that the app saves bookmarked tabs?
main.dart
List<String> bookmarked = [];
String introInfo = """ <h1>Introduction!</h1>
<p><strong><em>NOTE: The guides are U.S. specific but most information can be applied in most countries outside the U.S.</em></strong></p>
<p>The guides in this app will teach you the basics of personal finance.</p>
<p>Financial knowledge is something that is invaluable but the U.S. education system does not put much emphasis on it. If you are looking to get into personal finance, you're at the right place.</p>""";
void main() {
runApp(MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => MyApp(),
'/finTable': (context) => FinNav(),
'/disclaimer': (context) => Disclaimer(),
'/intro': (context) => GuideStyle(guideName: 'introduction',guideInfo: introInfo, isFav: bookmarked.contains('introduction'),),
'/budget': (context) => GuideStyle(guideName: 'budget',guideInfo: introInfo, isFav: bookmarked.contains('budget'),),
'/bookmark': (context) => Bookmarks(),
},
theme: ThemeData(fontFamily: 'Raleway'),
));
}
/* I have a stateless widget that shows all pages and navigates to one the user selects */
guidestyle.dart
class GuideStyle extends StatelessWidget {
String guideName;
String guideInfo;
Widget previous;
Widget next;
bool isFav;
GuideStyle({this.guideName,this.guideInfo, this.isFav });//this.next, this.previous});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color.fromRGBO(220, 20, 60, 1.0),
title: Text('Introduction'),
centerTitle: true,
elevation: 10.0,
actions: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(0.0,2.0,50.0,0.0),
child: MyStatefulWidget(isFav: isFav,name: guideName,),
),
],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Back'),
textColor: Colors.white,
color: Color.fromRGBO(220, 20, 60, 0.8),
),
),
Expanded(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: HtmlWidget(
guideInfo,
)
),
),
),
],
));
}
}
class MyStatefulWidget extends StatefulWidget {
bool isFav;
String name;
MyStatefulWidget({Key key, this.isFav, this.name}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: widget.isFav ? Icon(Icons.bookmark, color: Colors.black) : Icon(Icons.bookmark_border),
onPressed: () {
setState(() {
widget.isFav = !widget.isFav;
if(widget.isFav){
bookmarked.add(widget.name);
bookmarked = bookmarked;
}else{
bookmarked.remove(widget.name);
bookmarked = bookmarked;
}
});
},
),
],
);
}
}
As mentioned, the guidestyle.dart updates the list while the app is running but the list is reset when the app is restarted.
I was looking into using sqflite but it seems overkill so I am unsure about my other options. Any help would be appreciated!
You can use the SharedPreferences package or any other method that is able to persist data between app launches. See this for options to persist data.
Options:
If you are using SharedPreferences the setStringList
method will suit your needs perfectly.
As a side note, the line bookmarked = bookmarked;
is useless.