Save Data to Shared Preferences in Flutter | Day 08 -
HTML-код
- Опубликовано: 9 сен 2024
- Kilo Loco shows you how build settings page and save all the selected values to Shared Preferences. In this tutorial, you will learn about the different types of List Tiles, converting enums to and from savable states, and retrieving stored data from shared preferences.
** Show Notes and Links **
Project Files:
github.com/Kil...
Kilo Loco on Social Media:
www.kiloloco.com
Discord - / discord
Twitter - / kilo_loco
Instagram - / kilo_loco
#flutter #dartlang #30DaysOfFlutter
You can store the enums as String or ListString as the toString of the values, and restore them using this mapping, as demonstrated here in a dartpad snippet:
void main() {
var inStorage = Language.Flutter.toString();
var string2Language = Language.values.asMap().map((k,v) => MapEntry("$v",v));
var inProg = string2Language[inStorage];
print(inProg == Language.Flutter);
}
enum Language { Flutter, Dart, JavaScript, Perl }
Hello kilo loco, greetings from Colombia, I just want to leave you my most sincere thanks and respect for making such high quality content, especially I take great advantage of your flutter publications, since it is what I am currently applying, thank you very much friend and God bless
it's interesting going through these and implementing null safety along the way too
this series helped me a lot with writing one of my SwiftUI apps in flutter for android, I learned so much, and it is only day 8, I'm actually excited now, thank you sir.
i guess Im kinda randomly asking but do anyone know a good website to stream new tv shows online ?
@Brady Kaiser Meh try Flixportal. just google after it:P -guillermo
@Guillermo Trace Thanks, I went there and it seems like they got a lot of movies there :D I really appreciate it!!
@Brady Kaiser Happy to help :D
Just stumbled upon your channel. Love the length of these videos. Great explanations. Thanks!
Thanks a lot for the tutorials, I'm enjoying and learning a lot. Can you please make a video on backing up & restoring data stored in sqflite and also exporting and importing of data through csv files.
Best to have those key strings as const String then. :)
Bravo!👌
And I want to re-emphasize... your videos are great! Thank you for doing them!
Glad you like them!
And thank you for TextButton!
best video please continue with flutter videos Thanks in advance subscribed
Thank you very clear !
idk why m getting an error at newValue... it doesn't show anything except red line ... any idea ??
It's an object error. Just change it from newValue to newValue as Gender.
I have a problem with null safety! You tell at 27:20 that programmingLanguages may be null, but do not handle null safety. You just go on and leave the subject unhandled. When I start my app it gives me a null object at the .map Like this:
Exception has occurred.
NoSuchMethodError (NoSuchMethodError: The method 'map' was called on null.
Receiver: null
Tried calling: map(Closure: (String) => ProgrammingLanguage))
At least it must start with an empty List of Strings.
Can I create a dummy var with empty List of Strings? Obvious not I get runtime error when doing this
const emptyLanguage = [];
final programmingLanguagesIndicies =
preferences.getStringList('programmingLanguages') ?? emptyLanguage;
I am stuck! Anyone who can explain to overcome this and come from the ground up?
This video series doesn't cover null safety as it was not on stable at the time and is still currently an opt in feature. You may be able to solve the problem by using the ? in a few places to accommodate for null safety, but I haven't used it yet personally in dart. If I had to guess, the code should look something like this:
final programmingLanguages = programmingLanguagesIndicies?
.map((stringIndex) => ProgrammingLanguage.values[int.parse(stringIndex)])
.toSet()
?? Set()
The idea here being that the programmingLanguagesIndicies is nullable and if the value is null, then return an empty Set
@@Kilo_Loco No its not working ..
if you are talking about the null safety in the settings, then try using the "??" like this. isEmployed: isEmployed ?? true); username: username ?? "it's null",
Try to click the continue debug button while debugging, then select the checkbox option in your simulator, then save. That's what I fix. Hopefully, it can help. I think the first run there is no saved data yet, so it catches the empty set and popup the warning. Follow my step, hope next time can run normally. If any better ways to fix it, please let me know. Cheers
Did you found the solution? i'm also stuck in here
Amazing stuff! Thank you so much.
Glad you liked it!
great video man thank you
Thanks so cool video ) Ez like ^) Uh Models Uh.
Thanks for this lesson, I was wondering if I change the values too many times do they keep the old records or do they overwrite them? And if it is kept and repeated, how can I delete the old values, or in other words, how can I replace the value according to the key? Thanks again.
kudos!!
after converting to apk release file installing on an android phone not working not retreiving the API. but working fine in emulator. what is the reason anybody??
y we didn't had any parameter in onchanged in checkboxlist
I am new to flutter, what is “dirty” in his code
I had a problem importing shared_preferences. When I added the import 'package:shared_preferences/shared_preferences.dart'; into my code, the app crashes and 60% of dart started eating my cpu.
Update: Flutter released version 2.5 and upgrading to it made things work normally again.
Thanks a lot for the tutorial, Can you please refactor the code to use a state management library like Riverpod or Bloc instead of using SetState? thanks
I'll be covering state management in the coming weeks 😉
bro, how do you get into the database?
4:15 lol ... "other"