No implementation found for method getAll on channel plugins.flutter.io/shared_preferences

[ad_1]

Facing an exception on my flutter app.

  • The app runs fine on android OS, but throws and exception when trying to run on IOS.

  • The app runs perfectly on android.

  • Facing this issue on the splash screen where i am trying to fetch the login status from the shared pref.

[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)

This is my Shared pref implementation.

import 'package:shared_preferences/shared_preferences.dart';

import '../model/login/county_list_model.dart';

class SharedPrefHelper {
  // Singleton pattern
  SharedPrefHelper._privateConstructor();

  static final SharedPrefHelper instance =
  SharedPrefHelper._privateConstructor();

  // SharedPreferences keys
  static const String orgIdKey = 'orgId';
  static const String extAppIdKey = 'extAppId';
  static const String shortCodeKey = 'shortCode';
  static const String isActiveKey = 'isActive';
  static const String name="countyName";
  static const String userNameKey = 'username';
  static const String passWordKey = 'password';

  // SharedPreferences instance
  late SharedPreferences _prefs;

  // Initialize SharedPreferences
  Future<void> init() async {
    _prefs = await SharedPreferences.getInstance();
  }

  // variables for the County properties
  int get orgId => _prefs.getInt(orgIdKey) ?? 0;

   String  get extAppId => _prefs.getString(extAppIdKey) ?? '';

  String get shortCode => _prefs.getString(shortCodeKey) ?? '';

  String get countyName => _prefs.getString(name) ?? '';
  String get getUsername => _prefs.getString(userNameKey) ?? '';
  String get getPassword => _prefs.getString(passWordKey) ?? '';

  int get isActive => _prefs.getInt(isActiveKey) ?? 0;

  // variable to get login status\
  bool get isLoggedIn => _prefs.getBool('login') ?? false;
  bool get isRememberMe => _prefs.getBool('isRememberMe') ?? false;

  // Function to save selected item's properties to shared preferences
  saveSelectedCountyItem(Item selected) async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setInt('orgId', selected.orgId);
    prefs.setString('extAppId', selected.extAppId);
    prefs.setString('shortCode', selected.shortCode);
    prefs.setInt('isActive', selected.isActive);
    prefs.setString('countyName', selected.name);
  }

  Future<bool> setLogInStatus(bool loginStatus) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.setBool('login', loginStatus);
  }
  Future<bool> setaccessToken(String token) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.setString('token', token);
  }

  Future<bool> setUserToken(String token) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.setString('usertoken', token);
  }

  Future<String> geuserToken() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    return pref.getString('usertoken') ?? "";
  }

  Future<String> getoken() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    return pref.getString('token') ?? "";
  }

  Future<bool?> getLogInStatus() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    return pref.getBool('login') ?? false;
  }
  Future<bool> setUserName(String username) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.setString('username', username);
  }

  Future<bool> setPassword(String password) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.setString('password', password);
  }
  Future<bool> setRememberMe(bool isRememberMe) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.setBool('isRememberMe', isRememberMe);
  }


}

This is the splash screen the app gets stuck.

import 'package:civitpermit/constants/color_constants.dart';
import 'package:civitpermit/view/dashboard/dash_board.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../utils/shared_pref.dart';
import '../../view_models/login/login_viewmodel.dart';
import 'login_screen.dart';

class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {

    final authViewModel =
        Provider.of<AuthenticationViewModel>(context, listen: false);
    authViewModel.authenticateClient('');
    _navigateToLogin();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: ColorConstants.primaryColor,
        body: Center(
            child: Image.asset(
          "assets/images/ic_logo.png",
          height: 60,
        )),
      ),
    );
  }

  _navigateToLogin() async {
    await Future.delayed(
      const Duration(milliseconds: 2000),
      () {},
    );

    SharedPrefHelper.instance.getLogInStatus().then((value) {
      if (value == false) {
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => LoginScreen(),
          ),
        );
      } else {
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => const DashboardScreen(),
          ),
        );
      }
    });
  }
}

[ad_2]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top