Flutter re-login it does not directs to the intended home page - android-studio

Flutter Logout directs to login page but after re-login it does not redirects to the intended home page.
I have used the below navigation for login
Navigator.of(context).pushReplacement(PageRouteBuilder(pageBuilder: (_, __, ___) => Home()));
My app is consist of three bottom navigation bar.Home Page is in second navigation bar. I have to logout from third navigation bar.
I have used the below code for logout
Navigator.pushNamedAndRemoveUntil(context, '/Login', (_) => false);
Also i have tried the codes too
Navigator.popAndPushNamed(context, "/Login");
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) => Login(),), (Route route) => false);
Please help me to find a solution.

This is my splash screen
class SplashState extends State<SplashScreen>{
bool isLogged = false;
startTime(){
return new Timer(Duration(milliseconds: 3000), navigateUser);
}
#override
void initState() {
super.initState();
getStoredValue();
}
void getStoredValue() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey('isLoggin')) {
prefs.setBool('isLoggin', false);
print("login set to False");
}
isLogged = prefs.getBool('isLoggin');
print("Status in splash -----------$isLogged");
startTime();
}
#override
Widget build(BuildContext context) {
SystemChrome.setEnabledSystemUIOverlays([]);
return Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(
gradient:LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
colorStyle.splashPink,
colorStyle.splashOrange,
],
)
),
child: Center(
child: Image.asset("assets/images/splash_logo.png", height: 85.0),
),
);
}
void navigateUser() {
if (isLogged == true) {
Navigator.of(context).pushReplacement(
PageRouteBuilder(pageBuilder: (_, __, ___) => Home()));
}
else {
Navigator.of(context).pushReplacement(
PageRouteBuilder(pageBuilder: (_, __, ___) => Login()));
}
}
}
Logout
void logoutUser()async{
LogoutUser objLog = await createPost(logoutUrl,body);
if(objLog.statusCode == 10000){
print(("response is $resp"));
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('isLoggin', false);
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Login()));
}
else showToast(objLog.message, duration: Toast.LENGTH_LONG);
}

Try to use shared preference to store as login key and check if it is true then go to main screen or else redirect to login screen.
First of all add shared preference dependency in your pubspec.yaml
dependencies:
flutter:
sdk: flutter
shared_preferences: ^0.5.6+2
SplashScreen.dart
This is first screen where we are check that if isLogin is true then go to HomeScreen or else redirect to LoginScreen
_autoLogin() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey('isLogin')) {
prefs.setBool('isLogin', false);
}
if (prefs.getBool('isLogin')) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
));
} else {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => LoginScreen(),
));;
}
}
LoginScreen.dart
_setIsLogin() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('isLogin', true);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
));
}
MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
onPressed: () {
_setIsLogin();
},
child: Text("LOGIN",
textAlign: TextAlign.center,
style: style.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 18.0)),
),
HomeScreen.dart
Set isLogin as false when click on Logout
_logout() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('isLogin', false);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => LoginScreen(),
));
}
I hope this can help you!

Related

save string and associated icon with shared prefs

I'm currently using the shared preferences to save a string list. Every time the page is opened, for every string entry from the list I create a list tile inside a list view. But now I don't want to only save the string, I even want to save an icon with it. But I have absolutely no idea on how to solve this
Here is my current code:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:trainings_app/widgets/alertbox_widget.dart';
import 'package:trainings_app/widgets/team_widget.dart';
class TeamScreen extends StatefulWidget {
#override
_TeamScreenState createState() => _TeamScreenState();
}
class _TeamScreenState extends State<TeamScreen> {
late SharedPreferences sharedPreferences;
List<String> teams = [];
IconData? currentIcon;
#override
void initState() {
tryFetchData();
super.initState();
}
void tryFetchData() async {
sharedPreferences = await SharedPreferences.getInstance();
if (!sharedPreferences.containsKey('teams')) {
sharedPreferences.setStringList('teams', []);
return;
}
teams = sharedPreferences.getStringList('teams') as List<String>;
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: teams.length,
itemBuilder: (context, index) {
return Team(
teams[index],
Icon(currentIcon),
() => removeTeam(teams[index]),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
newTeam();
},
child: Icon(
CupertinoIcons.add,
),
),
);
}
void newTeam() {
showDialog<Alertbox>(
context: context,
builder: (BuildContext context) {
return Alertbox('Namen auswählen:', addTeam);
},
);
}
void addTeam(String name, IconData? icon) {
if (name.isNotEmpty) {
setState(() {
currentIcon = icon;
teams.add(name);
});
}
Navigator.pop(context);
sharedPreferences.setStringList('teams', teams);
}
void removeTeam(String name) {
setState(() {
teams.remove(name);
});
sharedPreferences.setStringList('teams', teams);
}
}
class Team extends StatelessWidget {
final String name;
final Icon icon;
final Function remove;
const Team(this.name, this.icon, this.remove);
#override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 22),
child: ListTile(
leading: Icon(icon.icon),
contentPadding: EdgeInsets.symmetric(vertical: 8.0),
title: Text(
name,
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.w600,
),
),
trailing: IconButton(
icon: Icon(CupertinoIcons.delete),
onPressed: () => remove(),
),
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => TeamWidget(name, icon)));
},
),
);
}
}
you can use each Icon specific id instead of IconData and store it as a List of Json:
Json.encode({title:"test", icon:61668}
and then save it in sharedPref. after that you can recall it as follows:
Icon(IconData(**YOUR SELECTED ID**, fontFamily: 'MaterialIcons'));
check the id of each icon here: link
The other solution can be using images instead of icons! or using this site to convert image to font icon and use it as follows:
Icon(IconData(int.parse('0x${e90a}',
fontFamily: 'family name given in the link above'));

LiveQuery does not work, if there is ParseConnectivityProvider provided

I'm working on a Flutter app, that consists of a employee scheduler with a Parse Server background.The app works as the following:
1)User opens the app;
2)The app shows a list of cards of available employees (if there is some in the server database)
3)The user is able to edit, confirm or delete the employee availability.
The app is working. It means that I'm able to CRUD data also from my Parse Server as from my app screen.
The goal: Every data change in the database needs to refresh (realtime) the user screen, removing, deleting or updating the (changed) data itself.
My idea: use parse_server_sdk: ^2.0.1 as a dependency (pubspec.yaml) and implement Live Queries (as the github docs here)
The Problem: after compilation, despite my AndroidStudio don't crashes visually speaking, nor my application itself, it shows me (logs) that "LiveQuery does not work, if there is ParseConnectivityProvider provided".
Without problems(main.dart):
void main() async{
runApp(MyApp());
}
Causing problems:
void main() async{
//the following parameters are in other file (constants.dart)...they are working
Parse().initialize(
kParseApplicationId,
kParseServerUrl,
masterKey: kParseMasterKey,
clientKey: kParseClientKey,
debug: true,
liveQueryUrl: kLiveQueryUrl,
autoSendSessionId: true,
);
var dietPlan = ParseObject('DietPlan')
..set('Name', 'Ketogenic')
..set('Fat', 65);
await dietPlan.save();
var response = await dietPlan.save();
if (response.success) {
dietPlan = response.result;
}
//until here, the program works well.The data is inserted and retrieved, ok...but exactly here..the problem!
final LiveQuery liveQuery = LiveQuery(); //from here..this is causing problems
QueryBuilder<ParseObject> query =
QueryBuilder<ParseObject>(ParseObject('DietPlan'))
..whereEqualTo('intNumber', 1);
Subscription subscription = await liveQuery.client.subscribe(query);
subscription.on(LiveQueryEvent.delete, (value) {
print('*** DELETE ***: ${DateTime.now().toString()}\n $value ');
print((value as ParseObject).objectId);
print((value as ParseObject).updatedAt);
print((value as ParseObject).createdAt);
print((value as ParseObject).get('objectId'));
print((value as ParseObject).get('updatedAt'));
print((value as ParseObject).get('createdAt'));
});
runApp(MyApp());
}
I didn't found any solution googleing.Any at all. After many research, I've got (going deep in the libraries) that Parse.Initialize(...) have connectivityProvider as one of it's parameters, but I didn't fill that parameter.
Also, the problems starts exactly when I've inserted the the line:
final LiveQuery liveQuery = LiveQuery();
Could someone help me to solve this issue?Thanks in advance
In the following, I'm showing all my code(main.dart):
// Copyright 2018 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:intl/intl.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:marca_horario/model/data.dart';
import 'package:marca_horario/network_utils/data_utils.dart';
import 'package:http/http.dart';
import 'package:parse_server_sdk/parse_server_sdk.dart';
import 'package:marca_horario/constants.dart';
void myLQ() async {
Parse().initialize(
kParseApplicationId,
kParseServerUrl,
masterKey: kParseMasterKey,
clientKey: kParseClientKey,
debug: true,
liveQueryUrl: kLiveQueryUrl,
autoSendSessionId: true,
);
var dietPlan = ParseObject('DietPlan')
..set('Name', 'Ketogenic')
..set('Fat', 65);
await dietPlan.save();
var response = await dietPlan.save();
if (response.success) {
dietPlan = response.result;
}
final LiveQuery liveQuery = LiveQuery();
QueryBuilder<ParseObject> query =
QueryBuilder<ParseObject>(ParseObject('DietPlan'))
..whereEqualTo('intNumber', 1);
Subscription subscription = await liveQuery.client.subscribe(query);
subscription.on(LiveQueryEvent.delete, (value) {
print('*** DELETE ***: ${DateTime.now().toString()}\n $value ');
print((value as ParseObject).objectId);
print((value as ParseObject).updatedAt);
print((value as ParseObject).createdAt);
print((value as ParseObject).get('objectId'));
print((value as ParseObject).get('updatedAt'));
print((value as ParseObject).get('createdAt'));
});
}
void main() async{
Parse().initialize(
kParseApplicationId,
kParseServerUrl,
masterKey: kParseMasterKey,
clientKey: kParseClientKey,
debug: true,
liveQueryUrl: kLiveQueryUrl,
autoSendSessionId: true,
);
var dietPlan = ParseObject('DietPlan')
..set('Name', 'Ketogenic')
..set('Fat', 65);
await dietPlan.save();
var response = await dietPlan.save();
if (response.success) {
dietPlan = response.result;
}
final LiveQuery liveQuery = LiveQuery();
QueryBuilder<ParseObject> query =
QueryBuilder<ParseObject>(ParseObject('DietPlan'))
..whereEqualTo('intNumber', 1);
Subscription subscription = await liveQuery.client.subscribe(query);
subscription.on(LiveQueryEvent.delete, (value) {
print('*** DELETE ***: ${DateTime.now().toString()}\n $value ');
print((value as ParseObject).objectId);
print((value as ParseObject).updatedAt);
print((value as ParseObject).createdAt);
print((value as ParseObject).get('objectId'));
print((value as ParseObject).get('updatedAt'));
print((value as ParseObject).get('createdAt'));
});
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
supportedLocales: [const Locale('pt', 'BR')],
home: Home()
);
}
}
class Home extends StatefulWidget {
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
var _listTiles = List<String>();
Color _iconColor = Colors.black;
Color standardIconColor = Colors.black;
Color alternateIconColor = Colors.green;
TextEditingController _nameController = TextEditingController();
var _scaffoldKey = new GlobalKey<ScaffoldState>();
String standardTileTitle = "Adicione um horário disponível...";
String _titleTile = "Adicione um horário disponível...";
String _tileSubtitle = "Edite o nome do funcionário...";
int _selectedIndexBottomNavBar = 0;
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
#override
Widget build(BuildContext context) {
return RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: () async {
setState(() {
});},
child: Scaffold(
key: _scaffoldKey,
floatingActionButton: FloatingActionButton(
onPressed: () {
invokeDatePicker();
},
child: Icon(Icons.add),
),
bottomNavigationBar: bottomNavigationBar(),
appBar: AppBar(
title: Text('Marca Horário'),
),
// body: Center(
// child: Text('Hello World'),
body: bodyStartScreen()
),
);
}
Widget bottomNavigationBar(){
void _onItemTapped(int index) {
setState(() {
_selectedIndexBottomNavBar = index;
});
print(_selectedIndexBottomNavBar);
if(_selectedIndexBottomNavBar == 2){
DatePicker.showDateTimePicker(context,
showTitleActions: true,
minTime: DateTime(2020, 1, 1),
maxTime: DateTime(2021, 12, 31),
onChanged: (date) {
print('change $date');
},
onConfirm: (date) {
print('confirm $date');
_listTiles.add(DateFormat.yMMMEd('pt_BR').add_Hm().format(date).toString());
_titleTile = DateFormat.yMMMEd('pt_BR').add_Hm().format(date).toString();
setState(() {
});
},
currentTime: DateTime.now(),
locale: LocaleType.pt);
}
}
return BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Funcionário',
),
BottomNavigationBarItem(
icon: Icon(Icons.access_time),
label: 'Marcar',
),
],
currentIndex: _selectedIndexBottomNavBar,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
);
}
Widget bodyStartScreen(){
return Column(
children: [
//the main title of the screen
Padding(
padding: EdgeInsets.all(16.0),
child: Text("Horários Possíveis",
style: TextStyle(
fontSize: 18.0
),
),
),
//gets available employees and datetimes from the server
FutureBuilder(builder: (context,snapshot){
if (snapshot.data != null) {
List<Data> dataList = snapshot.data;
return Expanded(
child: ListView.builder(
itemBuilder: (_, position) {
return Card(
child: ListTile(
title: Text(dataList[position].dateTime),
subtitle: Text(dataList[position].employee),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(icon: Icon(Icons.edit), onPressed: () {
//Show dialog box to update item
showUpdateDialog(dataList[position]);
}),
IconButton(icon: Icon(Icons.check_circle, color: Colors.green,), onPressed: () {
}),
//Show dialog box to delete item
IconButton(icon: Icon(Icons.delete), onPressed: () {
deleteData(dataList[position].objectId);
}),
],
),
),
);
},
itemCount: dataList.length,
),
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
future: getDataList(),
),
Divider(
color: Colors.black,
),
scheduleTile()
],
);
}
void invokeDatePicker(){
DatePicker.showDateTimePicker(context,
showTitleActions: true,
minTime: DateTime(2020, 1, 1),
maxTime: DateTime(2021, 12, 31),
onChanged: (date) {
print('change $date');
},
onConfirm: (date) {
print('confirm $date');
_listTiles.add(DateFormat.yMMMEd('pt_BR').add_Hm().format(date).toString());
_titleTile = DateFormat.yMMMEd('pt_BR').add_Hm().format(date).toString();
setState(() {
});
},
currentTime: DateTime.now(),
locale: LocaleType.pt);
}
void showUpdateDialog(Data data) {
_nameController.text = data.employee;
showDialog(context: context,
builder: (_) => AlertDialog(
content: Container(
width: double.maxFinite,
child: TextField(
controller: _nameController,
decoration: InputDecoration(
labelText: "Atualizar funcionário disponível",
),
),
),
actions: <Widget>[
FlatButton(onPressed: () {
Navigator.pop(context);
data.employee = _nameController.text;
updateData(data);
}, child: Text("Atualizar")),
FlatButton(onPressed: () {
Navigator.pop(context);
}, child: Text("Cancelar")),
],
)
);
}
Widget scheduleTile(){
return Padding(
padding: EdgeInsets.only(bottom: 80.0),
child: Card(
color: Colors.grey,
child: ListTile(
title: Text(_titleTile),
subtitle: Text(_tileSubtitle),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(
Icons.edit,
color: standardIconColor,
size: 20.0,
),
onPressed: () {
setState(() {
employeeAvailable();
});
},
),
IconButton(
icon: Icon(
Icons.check_circle_outline,
color: _iconColor,
size: 20.0,
),
onPressed: () {
setState(() {
(_titleTile != standardTileTitle) ? confirmSchedule() : fillTimeDialog();
});
},
)
],
),
),
),
);
}
void fillTimeDialog(){
showDialog(context: context,
builder: (_) => AlertDialog(
content: Container(
width: double.maxFinite,
child: Text("Insira o horário disponível!"),
),
actions: <Widget>[
FlatButton(onPressed: () {
Navigator.pop(context);
}, child: Text("OK")),
],
)
);
}
void employeeAvailable(){
showDialog(context: context,
builder: (_) => AlertDialog(
content: Container(
width: double.maxFinite,
child: TextField(
controller: _nameController,
decoration: InputDecoration(
labelText: "Funcionário",
),
),
),
actions: <Widget>[
FlatButton(onPressed: () {
Navigator.pop(context);
//addTodo();
setState(() {
_tileSubtitle = "Disponível: " + _nameController.text;
});
}, child: Text("Inserir")),
FlatButton(onPressed: () {
Navigator.pop(context);
setState(() {
_tileSubtitle = " ";
});
}, child: Text("Desfazer")),
],
)
);
}
void confirmSchedule(){
showDialog(context: context,
builder: (_) => AlertDialog(
content: Container(
width: double.maxFinite,
child: Text("Confirma disponibilidade?")
),
actions: <Widget>[
FlatButton(onPressed: () {
Navigator.pop(context);
//addTodo();
addData();
setState(() {
_iconColor = alternateIconColor;
_tileSubtitle = "Disponível: " + _nameController.text;
});
}, child: Text("Confirma")),
FlatButton(onPressed: () {
Navigator.pop(context);
setState(() {
_iconColor = standardIconColor;
_tileSubtitle = " ";
});
}, child: Text("Não")),
],
)
);
}
void addData() {
_scaffoldKey.currentState.showSnackBar(SnackBar(content: Row(
children: <Widget>[
Text("Adicionando informações..."),
CircularProgressIndicator(),
],
mainAxisAlignment: MainAxisAlignment.spaceBetween,
),
duration: Duration(minutes: 1),
));
Data data = Data(employee: _tileSubtitle, dateTime: _titleTile);
DataUtils.addData(data)
.then((res) {
_scaffoldKey.currentState.hideCurrentSnackBar();
Response response = res;
if (response.statusCode == 201) {
//Successful
_nameController.text = "";
_scaffoldKey.currentState.showSnackBar(SnackBar(content: Text("Informações disponibilizadas!"), duration: Duration(seconds: 1),));
setState(() {
//Update UI
});
}
});
}
void deleteData(String objectId) {
_scaffoldKey.currentState.showSnackBar(SnackBar(content: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text("Excluindo disponibilidade..."),
CircularProgressIndicator(),
],
),
duration: Duration(minutes: 1),
),);
DataUtils.deleteData(objectId)
.then((res) {
_scaffoldKey.currentState.hideCurrentSnackBar();
Response response = res;
if (response.statusCode == 200) {
//Successfully Deleted
_scaffoldKey.currentState.showSnackBar(SnackBar(content: (Text("Disponibilidade excluída!")),duration: Duration(seconds: 1),));
setState(() {
});
} else {
//Handle error
}
});
}
void updateData(Data data) {
_scaffoldKey.currentState.showSnackBar(SnackBar(content: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text("Atualizando disponibilidade..."),
CircularProgressIndicator(),
],
),
duration: Duration(minutes: 1),
),);
DataUtils.updateData(data)
.then((res) {
_scaffoldKey.currentState.hideCurrentSnackBar();
Response response = res;
if (response.statusCode == 200) {
//Successfully Deleted
_nameController.text = "";
_scaffoldKey.currentState.showSnackBar(SnackBar(content: (Text("Disponibilidade atualizada!"))));
setState(() {
});
} else {
//Handle error
}
});
}
Future <List<Data>> getDataList() async{
List<Data> dataList = [];
Response response = await DataUtils.getDataList();
print("Code is ${response.statusCode}");
print("Response is ${response.body}");
if (response.statusCode == 200) {
var body = json.decode(response.body);
var results = body["results"];
for (var data in results) {
dataList.add(Data.fromJson(data));
}
} else {
//Handle error
}
return dataList;
}
}
data_utils.dart:
import 'dart:convert';
import 'package:http/http.dart';
import 'package:marca_horario/model/data.dart';
import 'package:marca_horario/constants.dart';
class DataUtils {
//static final String _baseUrl = "https://parseapi.back4app.com/classes/";
static final String _baseUrl = baseUrl;
//CREATE
static Future<Response> addData(Data data) async {
String apiUrl = _baseUrl + "Data";
Response response = await post(apiUrl,
headers: {
'X-Parse-Application-Id': kParseApplicationId,
'X-Parse-REST-API-Key': kParseRestApiKey,
'Content-Type': 'application/json'
},
body: json.encode(data.toJson()),
);
return response;
}
//READ
static Future getDataList() async{
String apiUrl = _baseUrl + "Data";
Response response = await get(apiUrl, headers: {
'X-Parse-Application-Id' : kParseApplicationId,
'X-Parse-REST-API-Key' : kParseRestApiKey,
});
return response;
}
//UPDATE
static Future updateData(Data data) async{
String apiUrl = _baseUrl + "Data/${data.objectId}";
Response response = await put(apiUrl, headers: {
'X-Parse-Application-Id' : kParseApplicationId,
'X-Parse-REST-API-Key' : kParseRestApiKey,
'Content-Type' : 'application/json',
},
body: json.encode(data.toJson())
);
return response;
}
//DELETE
static Future deleteData(String objectId) async{
String apiUrl = _baseUrl + "Data/$objectId";
Response response = await delete(apiUrl, headers: {
'X-Parse-Application-Id' : kParseApplicationId,
'X-Parse-REST-API-Key' : kParseRestApiKey,
});
return response;
}
}
Problem solved!
My problem was:import 'package:parse_server_sdk/parse_server_sdk.dart';
It needs to be replaced by:import 'package:parse_server_sdk_flutter/parse_server_sdk.dart';
and in pubspec.yaml it's necessary to use only the correspondent dependency:
parse_server_sdk_flutter: ^2.0.1

How to fix WillPopScope() not working for me

I created two pages one is login and main page but i want close app from main page i am using Willpopscope but it is not working for me i have tried all things but the onwillpop method is not calling please help me thank you.
class main_page extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.amber),
home: my_page(),
);
}
}
class my_page extends StatefulWidget {
#override
_my_pageState createState() => _my_pageState();
}
class _my_pageState extends State<my_page> {
#override
void initState() {
check_login();
super.initState();
}
Future check_login() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
if (preferences.getBool("islogin") == false)
setState(() {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => MyApp(),
));
});
}
#override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async=>false,
child: Scaffold(
appBar: new AppBar(
title: Text(
"Home",
style: new TextStyle(color: Colors.white),
),
actions: <Widget>[
FlatButton(
onPressed: () async {
SharedPreferences sharedPreferences =
await SharedPreferences.getInstance();
setState(() {
sharedPreferences.setBool("islogin", false);
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => main_page(),
));
});
},
child: Text(
"Logout",
style: TextStyle(color: Colors.white, fontSize: 16.0),
))
],
),
),
);
}
}
I am also using Shredprefrences but i think it does not matter i want close app from my main_page please help me for this Thank you.
Just check out this answer and let me know if it works:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.amber),
home: SecondPage(),
);
}
}
class LoginPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: RaisedButton(
child: Text('Move to second page after Login'),
onPressed: () async {
SharedPreferences preferences =
await SharedPreferences.getInstance();
preferences.setBool("islogin", true);
// this will remove the login page after routing to the second page and then the stack will have only one page that is second page.
// so later you can use the willpopscape
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => SecondPage()),
ModalRoute.withName("/Home"));
},
)),
),
);
}
}
class SecondPage extends StatefulWidget {
#override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
#override
void initState() {
check_login();
super.initState();
}
Future check_login() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
if (preferences.getBool("islogin") == null)
//This above if statement will check if the parameter islogin is null then redirect to the login screeen
// else if the value is not null then i will not enter the if statement
setState(() {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => LoginPage(),
));
});
}
Future<bool> _onWillPop() async {
// This dialog will exit your app on saying yes
return (await showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Are you sure?'),
content: new Text('Do you want to exit an App'),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.of(context).pop(false),
child: new Text('No'),
),
new FlatButton(
onPressed: () => Navigator.of(context).pop(true),
child: new Text('Yes'),
),
],
),
)) ??
false;
}
#override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onWillPop,
child: Scaffold(
appBar: new AppBar(
title: Text(
"Home",
style: new TextStyle(color: Colors.white),
),
actions: <Widget>[
FlatButton(
onPressed: () async {
SystemNavigator.pop();
},
child: Text(
"Logout",
style: TextStyle(color: Colors.white, fontSize: 16.0),
))
],
),
),
);
}
}
Here is answer example_page code
class exmp extends StatefulWidget {
#override
_exmpState createState() => _exmpState();
}
class _exmpState extends State<exmp> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("example"),
),
body: Column(
children: <Widget>[
Center(
child: RaisedButton(onPressed: () {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => my_example(),
),
(route) => false);
}),
)
],
),
);
}
}
And this is my second page my_example
class my_example extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.amber),
home: my_examp(),
);
}
}
class my_examp extends StatefulWidget {
#override
_my_exampState createState() => _my_exampState();
}
class _my_exampState extends State<my_examp> {
Future<bool> onbackpress() async{
return true;
}
#override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: onbackpress,
child: Scaffold(
appBar: AppBar(
title: Text("my_eaxmple"),
),
),
);
}
}
The problem was i am pushing another page from my main page but i forget to remove first page for that i used the Navigator.of(context).pushAndRemoveUntil( MaterialPageRoute(builder: (context) => my_example()(route) => false); to remove the main page before navigate to another page
I was struggling with this problem, none of answers helped but finally I found the correct answer in BackButton widget source code.
When we use WillPopScope on top of our widget, we should use maybePop instead of pop method that forces the Navigator to pop the last pushed page in order to let the WillPopScope widget decides the next action.
Navigator.maybePop(context);
This is how you should use
Future<bool> _willPopCallback() async {
// await Show dialog of exit or what you want
// then
return true; //
}
//then use in the build like
WillPopScope(child: new Scaffold(), onWillPop: _willPopCallback)
I resolved this error as in my case I have a parent screen and in that, I have PageView. I was trying to call WillPopScope in one of my PageView and then I realized my parent have WillPopScope too.
That is why it is not calling WillPopScope of PageView
don't use on Scaffold(), wrap your MaterialApp as in the example
bool allowed = false;
return WillPopScope(
onWillPop: () async {
print('MJD POP TRYING TO GET BACK from login Page');
return Future.value(allowed);
},
child: MaterialApp(
title: 'Your App Title',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyLoginPage(title: 'Login Page'),
),
);

Hello does someone successfully added Bluetooth CPCL on your Flutter project?

I tried to print in a ZEBRA ez320 Bluetooth printer I found out that it only works using cpcl code, the good news is I have the sample code for cpcl but I don't know how to integrate it on my Flutter project.
Can someone help me out?
This github https://github.com/devinim/flutter-zsdk has Zebra SDK Kit Flutter Integration
code snippet
devices = await FlutterZsdk.discoverBluetoothDevices();
devices.forEach((d) {
print('Device: ${d.friendlyName} [${d.mac}]');
});
...
d.sendZplOverBluetooth(FLUTTER_LOGO_ZPL),
full example code https://github.com/devinim/flutter-zsdk/blob/master/example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_zsdk/flutter_zsdk.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const String FLUTTER_LOGO_ZPL = "^XA^CI28^PON^PW560^MNN^LL460^LH0,0^LT60" +
"^GFA,6150,6150,25,,gP01XFCgP03XF8gP07XF,gP0XFE,gO01XFC,gO03XF8,gO07XF,gO0XFE,gN01XFC,gN03XF8,gN0YF,gN0XFE,gM01XFC,gM03XF8,gM0YF,gL01XFE"
",gL01XFC,gL07XF8,gL0YF,gK01XFE,gK03XFC,gK07XF8,gK0YF,gJ01XFE,gJ03XFC,gJ07XF8,gJ0YF,gI01XFE,gI03XFC,gI07XF8,gI0YF,gH01XFE,gH03XFC,gH07XF8"
",gH0YF,gG01XFE,gG03XFC,gG07XF8,gG0YF,g01XFE,g03XFC,g07XF8,g0YF,Y01XFE,Y03XFC,Y07XF8,Y0YF,X01XFE,X03XFC,X07XF8,X0YF,W01XFE,W03XFC,W07XF8"
",W0YF,V01XFE,V03XFC,V07XF8,V0YF,U01XFE,U03XFC,U07XF8,U0YF,T01XFE,T03XFC,T07XF8,T0YF,S01XFE,S03XFC,S07XF8,S0YF,R01XFE,R03XFC,R07XF8"
",R0YF,Q01XFE,Q03XFC,Q07XF8,Q0YF,P01XFE,P03XFC,P07XF8,P0YF,O01XFE,O03XFC,O07XF8,O0YF,N01XFE,N03XFC,N07XF8,N0YF,M01XFE,M03XFC,M07XF8"
",M0YF,L01XFE,L03XFC,L07XF8,L0YF,K01XFE,K03XFC,K07XF8,K0YF,J01XFE,J03XFC,J07XF8,J0YF,I01XFE,I03XFC,I07XF8,I0XFE,001XFE,003XFCN03XF"
",007XF8N07XF,00XFEO0XFE,01XFCN01XFC,03XFCN03XF8,07XF8N07XF,0XFEO0XFE,1XFCN01XFC,3XF8N03XF8,7XF8N07XF,3WFEO0XFE,1WFCN01XFC,0WF8N03XF8"
",07VFO07XF,03UFEO0XFE,01UFCN01XFC,00UF8N03XF8,007TFO07XF,003SFEO0XFE,001SFCN01XFC,I0SF8N03XF8,I07RFO07XF,I03QFEO0XFE,I01QFCN01XFC,J0QF8N03XF8"
",J07PFO07XF,J03OFEO0XFE,J01OFCN01XFC,K0OF8N03XF8,K07NFO07XF,K03MFEO0XFE,K01MFCN01XFC,L0MF8N03XF8,L07LFO07XF,L03KFEO0XFE,L01KFCN01XFC,M0KF8N03XF8"
",M07JFO07XF,M03IFEO0XFE,M01IFCN01XFC,N0IF8N03XF8,N07FFO07XF,N03FEO0XFE,N01FCN01XFC,O0F8N03XF8,O07O07XF,O02O0XFE,X01XFC,X03XF8,X07XF,X0XFE,W01XFC"
",W03XF8,W07XF,W0XFE,V01XFC,V03XF8,V07XF,V0XFE,U01XFC,U03XF8,U07XF,U0XFE,T01XFC,T07XF8,T07XF,T03XF8,T01XFC,U0XFE,U07XF,U03XF8,U01XFC,V0XFE,V07XF"
",V03XF8,V01XFC,W0XFE,W07XF,W07XF8,W03XFC,X0XFE,X07XF,X07XF8,X03XFC,X01XFE,Y07XF,Y07XF8,Y03XFC,Y01XFE,g0YF,g07XF8,g03XFC,g01XFE,gG0YF,gG07XF8,gG03XFC"
",gG01XFE,gH0YF,gH07XF8,gH03XFC,gH01XFE,gI0YF,gI07XF8,gI03XFC,gI01XFC,gJ0XFE,gJ07XF,gJ03XF8,gJ01XFC,gK0XFE,gK07XF,gK03XF8,gK01XFC,gL0XFE,gL07XF,gL03XF8"
",gL01XFC,gM0XFE,gM07XF,gM03XF8,gM01XFC,gN0XFE,gN07XF,gN03XF8,gN01XFC,gO0XFE,gO07XF,gO03XF8,gO01XFC,gP0XFE,gP07XF,gR01VF8"
",^FS ^XZ";
List<ZebraBluetoothDevice> _devices = List();
#override
void initState() {
super.initState();
__init();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> __init() async {
List<ZebraBluetoothDevice> devices = List();
try {
devices = await FlutterZsdk.discoverBluetoothDevices();
devices.forEach((d) {
print('Device: ${d.friendlyName} [${d.mac}]');
});
} catch (e) {
showDialog(context: context, child: Text(e));
//throw e;
print('Error' + e);
}
if (!mounted) return;
setState(() {
_devices = devices;
});
}
String levelText = "Querying...";
_level(ZebraBluetoothDevice d) {
d.batteryLevel().then((t) {
setState(() {
levelText = t;
});
});
}
Widget _listPrinters() {
List<Widget> items = List();
if (_devices.length < 1) {
items.add(ListTile(
title: Text("Not found any or still searching"),
));
} else {
items.addAll([
ListTile(
title: Text("Found ${_devices.length} device(s)"),
),
SizedBox(height: 50),
]);
_devices.forEach((d) {
_level(d);
items.add(
ListTile(
title: Text(d.friendlyName),
subtitle: Text(d.mac + "[%${levelText}]"),
leading: IconButton(icon: Icon(Icons.list), onPressed: () => d.properties()),
trailing: IconButton(
icon: Icon(Icons.print),
onPressed: () => d.sendZplOverBluetooth(FLUTTER_LOGO_ZPL),
),
),
);
});
}
return ListView(
padding: EdgeInsets.all(24),
children: items,
);
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Zebra Plugin Example App'),
),
body: _listPrinters(),
),
);
}
}

Go to respective screen when FlatButton is pressed from AutoCompleteTextField

With autocomplete_textfield package of Flutter, I have made a search bar. When it returns the result as a container, I want to a button in it that'll push to the respected screen of the search result.
But how can I make the button dynamic so that it can go to the screen based on the search result?
For example, if the search result is Aishim, how can I get the button to go to AishimWidget() when pressed?
I have added the FlatButton to the result container. That goes to AishimWidget when pressed.
import 'package:autocomplete_textfield/autocomplete_textfield.dart';
import 'package:flutter/material.dart';
import 'package:smartdoctor/screens/aishim_widget.dart';
class SearchWidget extends StatefulWidget {
#override
State < StatefulWidget > createState() => _SearchWidgetState();
}
class _SearchWidgetState extends State < SearchWidget > {
#override
Widget build(BuildContext context) {
return new Scaffold(
body: SecondPage(),
);
}
}
class ArbitrarySuggestionType {
String name;
ArbitrarySuggestionType(this.name);
}
class SecondPage extends StatefulWidget {
#override
State < StatefulWidget > createState() => _SecondPageState();
}
class _SecondPageState extends State < SecondPage > {
List < ArbitrarySuggestionType > suggestions = [
new ArbitrarySuggestionType("Minamishima"),
new ArbitrarySuggestionType("Aishim"),
];
GlobalKey key =
new GlobalKey < AutoCompleteTextFieldState < ArbitrarySuggestionType >> ();
AutoCompleteTextField < ArbitrarySuggestionType > textField;
ArbitrarySuggestionType selected;
_SecondPageState() {
textField = new AutoCompleteTextField < ArbitrarySuggestionType > (
decoration: new InputDecoration(
hintText: "Search:", suffixIcon: new Icon(Icons.search)),
itemSubmitted: (item) => setState(() => selected = item),
key: key,
suggestions: suggestions,
itemBuilder: (context, suggestion) => new Padding(
child: new ListTile(
title: new Text(suggestion.name)),
padding: EdgeInsets.all(8.0)),
itemSorter: (a, b) => a.stars == b.stars ? 0 : a.stars > b.stars ? -1 : 1,
itemFilter: (suggestion, input) =>
suggestion.name.toLowerCase().startsWith(input.toLowerCase()),
);
}
#override
Widget build(BuildContext context) {
return new Scaffold(
body: new Column(children: [
new Padding(
padding: EdgeInsets.fromLTRB(0.0, 64.0, 0.0, 0.0),
child: new Card(
child: selected != null ?
new Column(children: [
new Container(
child: FlatButton(
onPressed: () => this.onButtonPressed(context))),
new ListTile(
title: new Text(selected.name)),
]) :
new Icon(Icons.home))),
]),
);
}
void onButtonPressed(BuildContext context) => Navigator.push(context, MaterialPageRoute(builder: (context) => AishimWidget()));
}
But it's not dynamic. Can I use any if statement for deciding which screen to go to? Or is there any better way of doing this?
You can use named-routes https://flutter.dev/docs/cookbook/navigation/named-routes
Map each page with a string
In onButtonPressed, use string
Navigator.pushNamed(context, '/second');
or in your case might looks like the following, you can pass any string you want like selected.name or suggestion.name
Navigator.pushNamed(context, suggestion.name);
define your route, map each page with a string
void main() {
runApp(MaterialApp(
title: 'Named Routes Demo',
// Start the app with the "/" named route. In this case, the app starts
// on the FirstScreen widget.
initialRoute: '/',
routes: {
// When navigating to the "/" route, build the FirstScreen widget.
'/': (context) => FirstScreen(),
// When navigating to the "/second" route, build the SecondScreen widget.
'/second': (context) => SecondScreen(),
},
));
}
full example code
import 'package:flutter/material.dart';
//void main() => runApp(MyApp());
void main() {
runApp(MaterialApp(
title: 'Named Routes Demo',
// Start the app with the "/" named route. In this case, the app starts
// on the FirstScreen widget.
initialRoute: '/',
routes: {
// When navigating to the "/" route, build the FirstScreen widget.
'/': (context) => FirstScreen(),
// When navigating to the "/second" route, build the SecondScreen widget.
'/second': (context) => SecondScreen(),
},
));
}
class FirstScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Center(
child: RaisedButton(
child: Text('Launch screen'),
onPressed: () {
// Navigate to the second screen using a named route.
Navigator.pushNamed(context, '/second');
},
),
),
);
}
}
class SecondScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
body: Center(
child: RaisedButton(
onPressed: () {
// Navigate back to the first screen by popping the current route
// off the stack.
Navigator.pop(context);
},
child: Text('Go back!'),
),
),
);
}
}

Resources