Исправлено падение приложения из-за попытки сделать запрос к бд, после закрытия соединения с ней

This commit is contained in:
Ivan Murashov
2017-09-26 11:35:30 +03:00
parent 9f2bf14199
commit 7a1dc84fa1
5 changed files with 40 additions and 40 deletions

View File

@@ -46,6 +46,7 @@
<activity <activity
android:name=".zxing.ScannerActivity" android:name=".zxing.ScannerActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme"/> android:theme="@style/AppTheme"/>
</application> </application>
</manifest> </manifest>

View File

@@ -40,11 +40,11 @@ logout(BuildContext context, SqliteHelper helper) async {
if (token != null) { if (token != null) {
getDeleteTokenRequest(token).then((response) { getDeleteTokenRequest(token).then((response) {
helper.clear().then((result) { helper.clear().then((result) {
helper.close().then((_) { // helper.close().then((_) {
Navigator.of(context).pop(); Navigator.of(context).pop();
Navigator.of(context).pop(); Navigator.of(context).pop();
pushRouteReplacement(context, new SplashScreen()); // Запускаем регистрацию pushRouteReplacement(context, new SplashScreen()); // Запускаем регистрацию
}); // });
}); });
}).catchError((error) { }).catchError((error) {
print(error.toString()); print(error.toString());
@@ -63,12 +63,12 @@ forceLogout(String token , BuildContext context) async {
SqliteHelper helper = new SqliteHelper(); SqliteHelper helper = new SqliteHelper();
helper.open().then((_) { helper.open().then((_) {
helper.clear().then((_) { helper.clear().then((_) {
helper.close().then((_) { // helper.close().then((_) {
while (Navigator.of(context).canPop()) { while (Navigator.of(context).canPop()) {
Navigator.of(context).pop(); Navigator.of(context).pop();
} }
pushRouteReplacement(context, new SplashScreen()); pushRouteReplacement(context, new SplashScreen());
}); // });
}); });
}); });
}).catchError((error) { }).catchError((error) {
@@ -117,8 +117,8 @@ startScanner(BuildContext context, String app, SqliteHelper helper) async {
} }
}); });
helper.close().then((_){ // helper.close().then((_){
helper = null; // helper = null;
platform.invokeMethod('getEndpoint').then((endpoint) { platform.invokeMethod('getEndpoint').then((endpoint) {
platform.invokeMethod('getAppToken').then((appToken) async { platform.invokeMethod('getAppToken').then((appToken) async {
platform.invokeMethod('startScanner', { platform.invokeMethod('startScanner', {
@@ -126,13 +126,11 @@ startScanner(BuildContext context, String app, SqliteHelper helper) async {
'url': endpoint, 'url': endpoint,
'appToken': appToken, 'appToken': appToken,
'locale': Intl.defaultLocale, 'locale': Intl.defaultLocale,
'color': Resources 'color': Resources.getPrimaryColor(app).value
.getPrimaryColor(app)
.value
});
}); });
}); });
}); });
// });
} }
} }
} }

View File

@@ -137,7 +137,4 @@ class SqliteHelper {
Future clear() async { Future clear() async {
return await db.delete(tableSession, where: null); return await db.delete(tableSession, where: null);
} }
Future close() async => db.close();
} }

View File

@@ -28,10 +28,7 @@ class MenuItem {
class SettingsState extends BaseState<SettingsScreen> { class SettingsState extends BaseState<SettingsScreen> {
List<MenuItem> menuItems = [ List<MenuItem> menuItems;
new MenuItem('', ''),
new MenuItem('', '')
];
bool returnToScanner; bool returnToScanner;
@@ -42,14 +39,22 @@ class SettingsState extends BaseState<SettingsScreen> {
} }
@override Widget build(BuildContext ctx) { @override Widget build(BuildContext ctx) {
if (menuItems == null) {
menuItems = [
new MenuItem('', ''),
new MenuItem('', '')
];
helper.getSettings().then((info) { helper.getSettings().then((info) {
setState(() { setState(() {
menuItems[0].title = StringsLocalization.currency(); menuItems[0].title = StringsLocalization.currency();
menuItems[1].title = StringsLocalization.locale(); menuItems[1].title = StringsLocalization.locale();
menuItems[0].selectedValue = info['currency'].toString(); menuItems[0].selectedValue = info['currency'].toString();
menuItems[1].selectedValue = info['locale'] == null ? Intl.defaultLocale : info['locale'].toString(); menuItems[1].selectedValue =
info['locale'] == null ? Intl.defaultLocale : info['locale']
.toString();
}); });
}); });
}
return new WillPopScope(onWillPop: onWillPop, child: getMainWidget()); return new WillPopScope(onWillPop: onWillPop, child: getMainWidget());
} }
@@ -72,11 +77,16 @@ class SettingsState extends BaseState<SettingsScreen> {
List<Widget> getSettings() { List<Widget> getSettings() {
List<Widget> widgets = new List(); List<Widget> widgets = new List();
if (menuItems != null) {
for (int i = 0; i < menuItems.length; i++) { for (int i = 0; i < menuItems.length; i++) {
if (menuItems[i].selectedValue != '') { if (menuItems[i].selectedValue != '') {
widgets.add(getSettingsItem(() => onPressed(menuItems.indexOf(menuItems[i])), widgets.add(
getSettingsItem(() => onPressed(menuItems.indexOf(menuItems[i])),
menuItems[i].title, menuItems[i].title,
i == 0 ? getCurrencyTitle(int.parse(menuItems[i].selectedValue)) : getLocaleTitle(menuItems[i].selectedValue))); i == 0 ? getCurrencyTitle(
int.parse(menuItems[i].selectedValue)) : getLocaleTitle(
menuItems[i].selectedValue)));
}
} }
} }
return widgets; return widgets;
@@ -105,8 +115,10 @@ class SettingsState extends BaseState<SettingsScreen> {
void onPressed(int position) { void onPressed(int position) {
switch (position) { switch (position) {
case 0 : case 0 :
menuItems = null;
return pushRoute(context, new CurrenciesScreen(helper, app)); return pushRoute(context, new CurrenciesScreen(helper, app));
case 1 : case 1 :
menuItems = null;
return pushRoute(context, new LanguagesScreen(helper, app)); return pushRoute(context, new LanguagesScreen(helper, app));
} }
} }

View File

@@ -129,7 +129,7 @@ class _SplashScreenState extends BaseState<SplashScreen> {
getCheckTokenStatusRequest(token).then((statusResponse) { getCheckTokenStatusRequest(token).then((statusResponse) {
handleStatusResponse(statusResponse, helper); handleStatusResponse(statusResponse, helper);
}).catchError((error) { }).catchError((error) {
handleError(error.toString()); print(error.toString());
}); });
} }
} }
@@ -179,7 +179,7 @@ class _SplashScreenState extends BaseState<SplashScreen> {
clearToken(response, helper); clearToken(response, helper);
} }
}).catchError((error) { }).catchError((error) {
handleError(error.toString()); print(error.toString());
}); });
} }
@@ -191,16 +191,8 @@ class _SplashScreenState extends BaseState<SplashScreen> {
Navigator.of(context).pop(); Navigator.of(context).pop();
pushRouteReplacement(context, new RegistrationScreen(helper, app)); pushRouteReplacement(context, new RegistrationScreen(helper, app));
}).catchError((error) { }).catchError((error) {
handleError(error.toString());
});
});
}
/// Закрываем соединение, логируем ошибку.
void handleError(String error) {
helper.close().then((_) {
print(error.toString()); print(error.toString());
return false; });
}); });
} }
} }