I want to navigate from the login screen to the home screen into my flutter app. My app is working fine when testing it on an emulator or virtual device, but on creating build release apk, navigation to Home does not work. On the signup or login page, navigation does not work. My main file is:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
String isLogin = prefs.getString('navigateToHome');
prefs.setString('is_mechanic', '1');
Widget _defaultHome = new LoginUI();
// isLogin = 'false';
if (isLogin == 'true') {
//If the user is an loggedin
print('home page');
_defaultHome = new Home();
} else {
// If the user is not loggedin
print('LoginUI page');
_defaultHome = new LoginUI();
}
runApp(MaterialApp(
title: 'Navigation Basics',
//navigateToHome
// home: FirstRoute(), //android device
home: _defaultHome,
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Colors.black,
),
)
);
}
The login/signup page from where I want to navigate to the homepage:
Future<loginModel> signUpUserOrMechanic(String email, String pass,
String cellPhone, String name, String workshop) async {
final prefs = await SharedPreferences.getInstance();
var userOrMechanic = prefs.getString('is_mechanic');
var nick = name.substring(0, 2);
var uri = new Uri.http(url, "/API/Signup/get", {
"Email": email,
"Password": pass,
"PhoneNumber": cellPhone,
"ismechanic": userOrMechanic,
"Name": name,
"WorkshopAddress": workshop,
"NickName": nick,
"Latitude": '',
"Longitude": '',
"WorkshopName": '',
});
final response = await http.get(uri);
if (response.statusCode == 200) {
final jsonObj = jsonDecode(response.body);
var responseObject = jsonDecode(response.body);
var user = loginModel.fromJson(responseObject);
// print('${user.clientOrMechanic} is ${user.alias}');
// print('${user.clientOrMechanic} is awesome');
prefs.setString('phone_no', cellPhone);
if (user.clientOrMechanic == "Already Registered") {
Utility.getInstance().showAlertDialog(
navigatorKey.currentState.overlay.context,
'System Alert!',
'You are already registered.Thanks!');
} else if (user.clientOrMechanic == "Registration Successful") {
prefs.setString('navigateToHome', 'true');
navigatorKey.currentState.push(MaterialPageRoute(
builder: (context) => Home(),
));
// Get.to(Home());
} else {
Utility.getInstance().showAlertDialog(
navigatorKey.currentState.overlay.context,
'Error!',
'Failed to Register. Please try again or contact to support. Thanks!');
}
return loginModel.fromJson(json.decode(response.body));
} else {
// If the server did not return a 201 CREATED response,
// then throw an exception.
Utility.getInstance().showAlertDialog(
navigatorKey.currentState.overlay.context,
'Error!',
'Failed to Register.Please try again.Thanks!');
throw Exception('Failed to login.');
}
}
Any help?