Ad

Shared Preferences Must Be Initialized Error

- 1 answer

In flutter I have faced with my below error about non-nullable variable. I am using shared preferences package for storing user info in device. I added the error in detail below:

Error: The non-nullable variable 'localStorage' must be initialized. Try adding an initializer expression.

shared_preferences: ^2.0.9

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  runApp(MyApp());
}

**SharedPreferences localStorage;**

TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();

class MyApp extends StatelessWidget {
  static Future init() async {
    localStorage = await SharedPreferences.getInstance();
  }

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

class MyHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Padding(
      padding: const EdgeInsets.all(30.0),
      child: Center(
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 200),
            ),
            Container(
              margin: EdgeInsets.symmetric(vertical: 10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'E-mail Id',
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  TextField(
                      controller: emailController,
                      obscureText: false,
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        fillColor: Colors.grey,
                        filled: true,
                      )),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.symmetric(vertical: 10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'Password :',
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  TextField(
                      controller: passwordController,
                      obscureText: false,
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        fillColor: Colors.grey,
                        filled: true,
                      )),
                ],
              ),
            ),
            ElevatedButton(
              onPressed: save,
              child: Text('Login'),
            ),
            Padding(
              padding: EdgeInsets.only(top: 50),
            ),
            if (localStorage != null)
              Padding(
                padding: const EdgeInsets.all(15.0),
                child: Text(
                  "User Logged in!!! ->  Email Id: ${localStorage.get('email')}  Password: ${localStorage.get('password')}",
                  style: TextStyle(fontSize: 20),
                ),
              ),
          ],
        ),
      ),
    ));
  }
}

save() async {
  await MyApp.init();
  localStorage.setString('email', emailController.text.toString());
  localStorage.setString('password', passwordController.text.toString());
}
Ad

Answer

In Flutter with Dart null-safety (Dart versions 2.12+) variables are required to always have a value, unless you specifically declare them as perhaps being null.

For your use case above, you could declare your global localStorage using the late keyword:

late SharedPreferences localStorage;

This tells Flutter that "I will assign this variable a non-null value prior to use, I promise."

You can fulfill this promise in main() (which I've made async, btw):

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // ↑ Req'd for using SharedPrefs (MethodChannel operations) prior to runApp
  localStorage = await SharedPreferences.getInstance();
  runApp(const MyApp());
}

Then you can use your global variable localStorage as expected.

Ad
source: stackoverflow.com
Ad