londonappbrewery / Flutter-Course-Resources

Learn to Code While Building Apps - The Complete Flutter Development Bootcamp
https://www.appbrewery.co
10.29k stars 3.07k forks source link

Flutter : Getting shared preferences crashes app #15

Open TarikApp opened 4 years ago

TarikApp commented 4 years ago

I want to use SharedPreferences in main class, but my application typically crashes(white page blocked) I added shared_preferences: ^0.5.7 to pubspec.yaml I have no error with flutter doctor

Thanks in Advance


import 'package:flutter/material.dart'; import 'package:newsapp/screens/onboarding.dart'; import 'screens/home_screen.dart'; import 'package:shared_preferences/shared_preferences.dart';

void main() async{

SharedPreferences prefs = await SharedPreferences.getInstance(); bool seen=prefs.getBool('seen'); Widget _screen; _screen = OnBoarding(); runApp( NewsApp( _screen) );

}

class NewsApp extends StatelessWidget {

final Widget _screen; NewsApp(this._screen);

@override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: _screen, ); } }