[ad_1]
so I am at present engaged on an attendance app and but to know how sharedpreferences works so I am struggling to maintain person logged in so long as they don’t seem to be manually logout. Extra confusingly, my app is utilizing PHP API because the backend.
This is the code for predominant.dart
void predominant() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences preferences = await SharedPreferences.getInstance();
var lg_username = preferences.getString('lg_username');
var lg_password = preferences.getString('lg_password');
bool? isLoggedin = preferences.getBool('isLoggedin');
person.lg_username = lg_username;
person.lg_password = lg_password;
person.lat = 0;
person.lengthy = 0;
print(person.lg_username);
print(person.lg_password);
// runApp(MaterialApp(dwelling: lg_username == null ? const MyApp() : const HomePage()));
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : tremendous(key: key);
@override
Widget construct(BuildContext context) {
return const MaterialApp(
// debugShowCheckedModeBanner: false,
dwelling: Splash(),
);
}
}
This operate in Splash() will skip login web page if person already login
_navigatetohome() async {
await Future.delayed(const Period(milliseconds: 1500), () {
if (person.lg_username == null){
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => const LoginPage()));
}else{
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => const HomePage()));
}
});
_login operate in login web page ought to save the information in person.dart
Future _login() async {
if (unameController.textual content == username && passController.textual content == password) {
Fluttertoast.showToast(
msg: 'Login Profitable',
backgroundColor: Colours.inexperienced,
textColor: Colours.white,
toastLength: Toast.LENGTH_SHORT,
);
SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setString('lg_username', unameController.textual content);
preferences.setString('lg_password', passController.textual content);
preferences.setBool('isLoggedin', true);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const HomePage(),
),
);
} else {
Fluttertoast.showToast(
msg: 'Username or Password is invalid!',
backgroundColor: Colours.pink,
textColor: Colours.white,
toastLength: Toast.LENGTH_SHORT,
);
}
}
person.dart the place I saved person login knowledge to be ship to the API
class person{
static String? lg_username;
static String? lg_password;
static bool? isLoggedin;
static double lat = 0;
static double lengthy = 0;
}
TodayPage() is the primary web page of the app and logged in person will likely be directed right here proper after the splash display.
Future getUser() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
person.lg_username = preferences.getString('lg_username');
});
}
Future displayLg_name() async {
var url = Uri.http(
"192.168.68.216", '/ump_attendance_2/username.php', {'q': '{http}'});
var response = await http.publish(url, physique: {
"lg_username": person.lg_username,
});
if (response.physique.isNotEmpty) {
return json.decode(response.physique);
}
}
BUT, it return solid error within the displayLg_name() above. And my app wouldnt show the title of the person which is able to retrieve from the database. I assumed I already reserve it within the person.dart however after I’m making an attempt to open the app once more after login, it appears the API doesnt retrieve any knowledge from my app. How do I repair this?
Thanks upfront!
[ad_2]
