在 Flutter 中重置密码 Rest Api
Reset Password Rest Api In Flutter
import 'dart:convert';
ResetPass resetPassFromJson(String str) => ResetPass.fromJson(json.decode(str));
String resetPassToJson(ResetPass data) => json.encode(data.toJson());
class ResetPass {
ResetPass({
this.status,
this.msg ,
});
String status;
String msg;
factory ResetPass.fromJson(Map<String, dynamic> json) => ResetPass(
status: json["status"],
msg: json["msg"] ,
);
Map<String, dynamic> toJson() => {
"status": status,
"msg": msg,
};
}
我正在使用 WordPress 的重置密码 API,成功的重置操作显示
SuccessFull Result different JSON and on the error response it shows the different JSON Error Result 如果有人让我知道如何在 Flutter 模型中处理它,请告诉我 class。我目前是这样做的
Model Class
您可以复制粘贴运行下面的完整代码
如果 null
,则可以将 null
分配给 property
例如 error: json["error"] == null ? null : json["error"],
代码片段
ResetPass resetPassFromJson(String str) => ResetPass.fromJson(json.decode(str));
String resetPassToJson(ResetPass data) => json.encode(data.toJson());
class ResetPass {
ResetPass({
this.status,
this.msg,
this.error,
});
String status;
String msg;
String error;
factory ResetPass.fromJson(Map<String, dynamic> json) => ResetPass(
status: json["status"],
msg: json["msg"] == null ? null : json["msg"],
error: json["error"] == null ? null : json["error"],
);
Map<String, dynamic> toJson() => {
"status": status,
"msg": msg == null ? null : msg,
"error": error == null ? null : error,
};
}
输出
I/flutter (13259): msg: ok message error: null
I/flutter (13259): msg: null error: error message
完整代码
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
ResetPass resetPassFromJson(String str) => ResetPass.fromJson(json.decode(str));
String resetPassToJson(ResetPass data) => json.encode(data.toJson());
class ResetPass {
ResetPass({
this.status,
this.msg,
this.error,
});
String status;
String msg;
String error;
factory ResetPass.fromJson(Map<String, dynamic> json) => ResetPass(
status: json["status"],
msg: json["msg"] == null ? null : json["msg"],
error: json["error"] == null ? null : json["error"],
);
Map<String, dynamic> toJson() => {
"status": status,
"msg": msg == null ? null : msg,
"error": error == null ? null : error,
};
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
String jsonStringSuccess = '''
{
"status":"ok",
"msg":"ok message"
}
''';
String jsonStringError = '''
{
"status":"error",
"error":"error message"
}
''';
http.Response response = http.Response(jsonStringSuccess, 200);
if (response.statusCode == 200) {
ResetPass resetPassSuccess = resetPassFromJson(response.body);
print("msg: ${resetPassSuccess.msg} error: ${resetPassSuccess.error}");
}
http.Response responseError = http.Response(jsonStringError, 200);
if (responseError.statusCode == 200) {
ResetPass resetPassError = resetPassFromJson(responseError.body);
print("msg: ${resetPassError.msg} error: ${resetPassError.error}");
}
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
import 'dart:convert';
ResetPass resetPassFromJson(String str) => ResetPass.fromJson(json.decode(str));
String resetPassToJson(ResetPass data) => json.encode(data.toJson());
class ResetPass {
ResetPass({
this.status,
this.msg ,
});
String status;
String msg;
factory ResetPass.fromJson(Map<String, dynamic> json) => ResetPass(
status: json["status"],
msg: json["msg"] ,
);
Map<String, dynamic> toJson() => {
"status": status,
"msg": msg,
};
}
我正在使用 WordPress 的重置密码 API,成功的重置操作显示 SuccessFull Result different JSON and on the error response it shows the different JSON Error Result 如果有人让我知道如何在 Flutter 模型中处理它,请告诉我 class。我目前是这样做的
Model Class
您可以复制粘贴运行下面的完整代码
如果 null
,则可以将 null
分配给 property
例如 error: json["error"] == null ? null : json["error"],
代码片段
ResetPass resetPassFromJson(String str) => ResetPass.fromJson(json.decode(str));
String resetPassToJson(ResetPass data) => json.encode(data.toJson());
class ResetPass {
ResetPass({
this.status,
this.msg,
this.error,
});
String status;
String msg;
String error;
factory ResetPass.fromJson(Map<String, dynamic> json) => ResetPass(
status: json["status"],
msg: json["msg"] == null ? null : json["msg"],
error: json["error"] == null ? null : json["error"],
);
Map<String, dynamic> toJson() => {
"status": status,
"msg": msg == null ? null : msg,
"error": error == null ? null : error,
};
}
输出
I/flutter (13259): msg: ok message error: null
I/flutter (13259): msg: null error: error message
完整代码
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
ResetPass resetPassFromJson(String str) => ResetPass.fromJson(json.decode(str));
String resetPassToJson(ResetPass data) => json.encode(data.toJson());
class ResetPass {
ResetPass({
this.status,
this.msg,
this.error,
});
String status;
String msg;
String error;
factory ResetPass.fromJson(Map<String, dynamic> json) => ResetPass(
status: json["status"],
msg: json["msg"] == null ? null : json["msg"],
error: json["error"] == null ? null : json["error"],
);
Map<String, dynamic> toJson() => {
"status": status,
"msg": msg == null ? null : msg,
"error": error == null ? null : error,
};
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
String jsonStringSuccess = '''
{
"status":"ok",
"msg":"ok message"
}
''';
String jsonStringError = '''
{
"status":"error",
"error":"error message"
}
''';
http.Response response = http.Response(jsonStringSuccess, 200);
if (response.statusCode == 200) {
ResetPass resetPassSuccess = resetPassFromJson(response.body);
print("msg: ${resetPassSuccess.msg} error: ${resetPassSuccess.error}");
}
http.Response responseError = http.Response(jsonStringError, 200);
if (responseError.statusCode == 200) {
ResetPass resetPassError = resetPassFromJson(responseError.body);
print("msg: ${resetPassError.msg} error: ${resetPassError.error}");
}
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}