How to store device info in shared preference in flutter

393

To Save your info in memory, set sharedprefs using :

SharedPreferences prefs = await SharedPreferences.getInstance();
        if (Platform.isAndroid) {
          AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
          prefs.setString('deviceinfo', androidInfo.id});
          prefs.setString('deviceID', your_android_id_value});
          prefs.setString('deviceName', your_android_name_value});
        } else if (Platform.isIOS) {
          IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
          prefs.setString('deviceinfo', iosInfo.utsname.machine});
          prefs.setString('deviceID', your_ios_id_value});
          prefs.setString('deviceName', your_ios_name_value});
        }

To get info from memory using shared prefs :

SharedPreferences prefs = await SharedPreferences.getInstance();
     String info=prefs.getString("deviceinfo");
     String info=prefs.getString("deviceID");
     String info=prefs.getString("deviceName");
     print("info : ${info}");
     print("ID: ${deviceID}");
     print("Name: ${deviceName}");

This will help you.

Share:
393
Rutvik Gumasana
Author by

Rutvik Gumasana

flutterrun.com

Updated on December 14, 2022

Comments

  • Rutvik Gumasana
    Rutvik Gumasana over 1 year

    in this question i want to store device info into shared preferences becuase i want to access device info anywhere from application. Hope you understand the question. your small help can made my day :)

    Here is some code i've tried

          Future deviceinfo() async {
            if (Platform.isAndroid) {
              AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
              print("Hey Android User");
              print('Running on ${androidInfo.id}');
              print('Running on ${androidInfo.isPhysicalDevice}');
              print('Running on ${androidInfo.fingerprint}');
    //Here in this line error
              SharedPreferences devicepref = await SharedPreferences.getInstance();
              devicepref.setString('deviceinfo', androidInfo);
            } else if (Platform.isIOS) {
              IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
              print("Hey IOS User");
              print('Running on ${iosInfo.utsname.machine}');
            }
          }
    
    • Boby
      Boby over 4 years
      declare variable outside the deviceinfo() then set the variable
    • Rutvik Gumasana
      Rutvik Gumasana over 4 years
      can you please help me to do this. if you can please help me.