有解决此问题的解决方案吗?
堆栈跟踪:
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
#0 defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:73:7)
#1 defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:86:4)
#2 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
#3 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35) …Run Code Online (Sandbox Code Playgroud) 我正在获取 csrf 令牌并在控制台中打印响应数据,但如何使用响应数据使用户保持登录状态。我正在使用状态代码登录,即,如果状态代码为 200,则在此之后移动到登录状态,我想保持仅当用户想注销时才登录和注销
我看过很多例子,但没有一个对我的情况有帮助。
就我而言,我使用的是 csrf 令牌并且无法保持登录状态,并且我还使用了登录表单。
登录页面.dart
import 'dart:io';
import 'package:cookie_jar/cookie_jar.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jignasa/home_screen.dart';
import 'package:jignasa/logindata.dart';
import 'package:path_provider/path_provider.dart';
class LoginPage extends StatefulWidget {
static String tag = 'login-page';
@override
_LoginPageState createState() => new _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
LoginRequestData _loginData = LoginRequestData();
bool _validate = false;
bool _obscureText = true;
var username, password;
@override
Widget build(BuildContext context) {
return Scaffold(
// backgroundColor: Colors.white,
body: SingleChildScrollView(
child: …Run Code Online (Sandbox Code Playgroud) 我想判断要从哪个页面开始(实际上是登录页面和主页)。因此,我必须在首选项中阅读isLogin。主要如何做?
我绑了这些代码:
Future<Null> checkIsLogin() async {
String _token = "";
// If token exist already, then HomePage
SharedPreferences prefs = await SharedPreferences.getInstance();
_token = prefs.getString("token");
print('get token from prefs: ' + _token);
if (_token != "" && _token != null) {
// already login
print("alreay login.");
isLogin = true;
}
}
void main() {
App.init();
// if we have token then go to HomePage directly otherwise go to LoginPage.
Widget _defaultHome = new LoginPage();
checkIsLogin();
if (isLogin) {
_defaultHome = …Run Code Online (Sandbox Code Playgroud) 我对共享首选项有一些问题。如何通过关闭应用程序让用户登录?
我已经用这个代码存储了用户名:
setState(() {
preferences.setString('token', dataUser[0]['username']);
Navigator.of(context).pushAndRemoveUntil(
PageTransition(
type: PageTransitionType.rightToLeftWithFade,
child: HomePage()),
(Route<dynamic> route) => false);
});
Run Code Online (Sandbox Code Playgroud)
当我点击按钮登录时,它可以转到主页。为了确保用户名存储/不存储,我使用函数来获取我的令牌并获取该令牌。
我的令牌使用此代码获取我期望的内容:
String checkPref;
getPrefIdUser() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
setState(() {
checkPref = sharedPreferences.getString('token');
if (checkPref == null) {
Navigator.of(context).pushAndRemoveUntil(
PageTransition(
type: PageTransitionType.leftToRightWithFade,
child: LoginPage()),
(Route<dynamic> route) => false);
} else {
// Navigator.of(context).pushAndRemoveUntil(
// PageTransition(
// type: PageTransitionType.leftToRightWithFade,
// child: HomePage()),
// (Route<dynamic> route) => false);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我已经getPrefIdUser()在我的主页中放入了这样的 initState:
@override
void initState() { …Run Code Online (Sandbox Code Playgroud) 我有一个颤振应用程序。即使在退出应用程序后,我也想让用户保持登录状态。我有 main.dart 文件、login.dart 和dashboard.dart。如果用户尚未登录,我希望 main.dart 重定向到 login.dart,但如果找到共享首选项值,则直接重定向到仪表板。
sharedpreferences dart firebase firebase-authentication flutter