import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'Models/UserData.dart';
class LoginScreen extends StatefulWidget {
static const String id = '/login_screen';
@override
State<StatefulWidget> createState() {
return _LoginPageState();
}
}
class _LoginPageState extends State<LoginScreen> {
static var url = "url";
static BaseOptions options = BaseOptions(
baseUrl: url,
responseType: ResponseType.plain,
connectTimeout: 30000,
receiveTimeout: 30000,
validateStatus: (code) {
if (code >= 200) {
return true;
}
});
static Dio dio = Dio(options);
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
TextEditingController _emailController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
UserData userData;
@override
void initState() {
SystemChrome.setEnabledSystemUIOverlays([]);
userData = UserData();
super.initState();
}
Future<Map<String, dynamic>> _loginUser(String email, String password, String version) async {
try {
Options options = Options(
contentType: ContentType.parse('application/json'),
);
final Response response = await dio.post<Map<String, dynamic>>(url + '/users/login',
data: {'login': _emailController, 'pwd': _passwordController, 'version' :'2.0'}, options: options);
print(url + '/users/login');
if (response.statusCode == 200 || response.statusCode == 201) {
return json.decode(response.data);
} else if (response.statusCode == 401) {
throw Exception("Incorrect Email/Password");
} else
throw Exception('Authentication Error');
} on DioError catch (exception) {
if (exception == null ||
exception.toString().contains('SocketException')) {
throw Exception("Network Error");
} else if (exception.type == DioErrorType.RECEIVE_TIMEOUT ||
exception.type == DioErrorType.CONNECT_TIMEOUT) {
throw Exception(
"Could'nt connect, please ensure you have a stable network.");
} else {
return null;
}
}
}
@override
Widget build(BuildContext context) {
bool _isLoading = false;
String version = '2.0';
bool _obscureText = true;
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
width: MediaQuery
.of(context)
.size
.width,
height: MediaQuery
.of(context)
.size
.height / 2.5,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.white, Colors.white],
),
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(90),
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Spacer(),
Align(
alignment: Alignment.center,
child: Image.asset('assets/logo.png'),
),
Spacer(),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.only(bottom: 32, right: 32),
child: Text(
'Customer App',
style: TextStyle(color: Colors.red, fontSize: 18),
),
),
),
],
),
),
Container(
color: Colors.white,
height: MediaQuery
.of(context)
.size
.height / 1,
width: MediaQuery
.of(context)
.size
.width,
padding: EdgeInsets.only(top: 20),
child: Column(
children: <Widget>[
Container(
width: MediaQuery
.of(context)
.size
.width / 1.2,
height: 45,
padding:
EdgeInsets.only(top: 2, left: 16, right: 16, bottom: 2),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(50)),
color: Colors.white,
boxShadow: [
BoxShadow(color: Colors.red, blurRadius: 5)
]),
child: TextField(
controller: _emailController,
decoration: InputDecoration(
errorText: _isLoading ? 'Value Can\'t Be Empty' : null,
border: InputBorder.none,
icon: Icon(
Icons.email,
color: Colors.red,
),
hintText: 'Email',
),
keyboardType: TextInputType.emailAddress,
),
),
const SizedBox(
width: 42.0,
height: 20.0,
),
Container(
width: MediaQuery
.of(context)
.size
.width / 1.2,
height: 45,
padding:
EdgeInsets.only(top: 2, left: 16, right: 16, bottom: 2),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(50)),
color: Colors.white,
boxShadow: [
BoxShadow(color: Colors.red, blurRadius: 8)
]),
child: TextField(
obscureText: _obscureText,
controller: _passwordController,
inputFormatters: [LengthLimitingTextInputFormatter(10)],
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(
Icons.lock_open,
color: Colors.red,
),
hintText: 'Password',
),
keyboardType: TextInputType.text,
),
),
Container(
height: 45,
margin: EdgeInsets.only(top: 22),
width: MediaQuery
.of(context)
.size
.width / 1.2,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.all(Radius.circular(50))),
child: Center(
child: InkWell(
onTap: () async {
FocusScope.of(context).requestFocus(new FocusNode());
setState(() => _isLoading = true,);
Navigator.of(context)
.pushReplacementNamed('/home_screen');
var res = await _loginUser(
_emailController.text, _passwordController.text,version);
final UserData users = UserData.fromJson(res);
List<Map> items = json.decode("response.body");
List<UserData> listOfDesig = items.map((json) => UserData.fromJson(json)).toList();
if (listOfDesig != null) {
Navigator.of(context)
.pushReplacementNamed('/home_screen');
} else {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text("Wrong email")));
}
},
child: Text(
'Login'.toUpperCase(),
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold),
),
),
),
),
],
),
),
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.only(top: 20, right: 0),
child: Text(
'Forgot Password ?',
style: TextStyle(color: Colors.grey),
),
),
),
],
),
),
);
}
}
class UserData {
String customerid;
String profileid;
String branch;
String bookername;
String emailid;
String mobileno;
int cutofftime;
String ppnames;
String paymenttype;
String usertype;
int ptopCutoffTime;
String designation;
String designationid;
UserData({
this.customerid,
this.profileid,
this.branch,
this.bookername,
this.emailid,
this.mobileno,
this.cutofftime,
this.ppnames,
this.paymenttype,
this.usertype,
this.ptopCutoffTime,
this.designation,
this.designationid,
});
factory UserData.fromJson(Map<String, dynamic> json) => UserData(
customerid: json["customerid"],
profileid: json["profileid"],
branch: json["branch"],
bookername: json["bookername"],
emailid: json["emailid"],
mobileno: json["mobileno"],
cutofftime: json["cutofftime"],
ppnames: json["ppnames"],
paymenttype: json["paymenttype"],
usertype: json["usertype"],
ptopCutoffTime: json["ptopCutoffTime"],
designation: json["designation"],
designationid: json["designationid"],
);
}
我看到你的代码的方式,错误来自这个
@override
void initState() {
SystemChrome.setEnabledSystemUIOverlays([]);
userData = UserData(); //What cause the error.
super.initState();
}
为什么?因为在从服务器获得UserData类后,还没有将值放入该类中。如果身份验证有效,请尝试从服务器收集数据。使用userdata.fromjson(json.decode(response.body));
我在这里使用http而不是Dio,但请尝试通过以前的代码将其转换为Dio。
Future<UserData> getUserData() async{
final SharedPreferences prefs = await SharedPreferences.getInstance();
String email = prefs.getString('Email');
String session = prefs.getString('session');
var map = new Map<String, String>();
map["email"] = email;
map["sesscode"] = session;
var response = await http.post(new API().userdata, body: map);
var convertDataToJson = json.decode(response.body);
UserData usdata = UserData .fromJson(convertDataToJson); //THis solve your error
return usdata ;
}
我正试图将语音到文本的方法添加到我的应用程序中,以转换我的,但我有这个错误 所以有人能帮我吗!
当我将其更改为list时,出现以下错误: 这里是我的小部件构建: 对于API、模型和其他函数,您可以在未处理的异常中检查我的另一篇文章:键入“\u InternalLinkedHashMap”
我一直在学习如何构建一个类似Instagram的社交媒体应用程序的教程,尽管我的代码与教程中的代码相同,但当我试图显示用户的帖子时,我得到了这个错误: building FutureBuilder(脏,状态:_FutureBuilderState#0C840)引发了以下NosuchMethodeRor:方法“[]”是在NULL上调用的。接收者:null尝试调用:
我正在尝试制作一个可滚动的选项卡,但当我在我的片段布局中设置一个视图时,应用程序开始出现错误,因为错误inflating类为空。它在未添加视图时工作,但当我在布局中添加视图时,它会给我错误。我也按此处给出的那样添加了类,但它不起作用。我如何在这里修复这个问题是我的片段代码 片段包名称为包com。实例尼莱。另一个盒子。碎片片段名称歌曲。我怎样才能解决这个问题?错误 D/dalvikvm:GC_并发释
我试图使用API中的数据,但我需要的数据是为不同的请求找到的。在颤栗中有未来。通过这个,我可以请求API的必要部分。我的想法是,我正在努力为火车制定时间表。在这个列车时刻表中,我需要列车号、站台名和到达时间。我正在尝试按照文档和视频中所描述的方式执行所有操作,但我做不到,因为我遇到了一个错误 在这里提问之前,我试着做了这样的事情: 但是它不起作用,给我一个错误: 对null调用了方法“[]”。接收
错误:无法找到或加载主类M\AppData\local\android\sdk\cmdline-tools\lates\bin\...由:java.lang.ClassNotFoundException:M\AppData\local\Android\sdk\cmdline-tools\lates\bin\ 如何解决这个问题?