我是新手。我尝试了很多事情来获取数据并将数据导出到屏幕。 Response.statusCode = 200,但数据始终为空here。
Github:here
我认为来自Main-> future的错误:getPost(url,“ OAISJFAOISFJASF”)或生成器:(上下文,快照){...}
主要:
import 'package:flutter/material.dart';
import 'package:postapi/post_login.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: Home()
);
}
}
class Home extends StatelessWidget{
String url = "http://13.250.59.224:56321/api/users/login";
callAPI(){
Post post = Post();
createPost(post, url).then((response){
if(response.statusCode > 200) {
print(response.body);
}
else {
print(response.statusCode);
}
}).catchError((error){
print('error : $error');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body : FutureBuilder<Post>(
future: getPost(url, "OAISJFAOISFJASF"),
builder: (context, snapshot) {
callAPI();
if(snapshot.connectionState == ConnectionState.done) {
if(snapshot.hasError){
return Text("Error");
}
return Text('UserID JSON : ${snapshot.data.userID}');
}
else
return CircularProgressIndicator();
}
)
);
}
}
Post_Login:
import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
Future<Post> getPost(String url, String firebase_token) async{
Map<String, String> headers = {"Content-type": "application/json"};
String json = '{"grant_type":"firebase_token","firebase_token":"$firebase_token"}';
final response = await http.post(
url, headers: headers,
body: json);
return postFromJson(response.body);
}
Future<http.Response> createPost(Post post, String url) async{
final response = await http.post('$url',
headers: {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.authorizationHeader : ''
},
body: postToJson(post)
);
return response;
}
Post postFromJson(String str) {
final jsonData = json.decode(str);
return Post.fromJson(jsonData);
}
String postToJson(Post data) {
final dyn = data.toJson();
return json.encode(dyn);
}
class Post {
String userID;
String access_token;
String refresh_token;
String role;
Post({
this.userID,
this.access_token,
this.refresh_token,
this.role
});
factory Post.fromJson(Map<String, dynamic> json) => new Post(
userID: json["userID"],
access_token: json["description"],
refresh_token: json["refresh_token"],
role: json["role"],
);
Map<String, dynamic> toJson() => {
"userID": userID,
"access_token": access_token,
"refresh_token": refresh_token,
"role": role,
};
}
asdaskdasldhjalshfjlashflasdhjlaskjdaks; djaksfhjksbckasdasjdaskjdasjdklasjdlkasjdlkjglashfjklshfsadlfjsklfjsalkfjsalfjs; lfjasl
答案 0 :(得分:1)
只需检查一下我根据您的示例创建的示例。
import 'package:flutter/material.dart';
import 'package:sample_list/post_login.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(home: Home());
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
String url = "http://13.250.59.224:56321/api/users/login";
@override
void initState() {
super.initState();
callAPI();
}
callAPI() {
Post post = Post();
createPost(post, url).then((response) {
if (response.statusCode == 200) {
print(response.body);
} else {
print(response.statusCode);
}
}).catchError((error) {
print('error : $error');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder<Post>(
future: getPost(url, "OAISJFAOISFJASF"),
builder: (context, snapshot) {
//callAPI(); // It will get called every time for every build
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Text("Error");
}
return Text('UserID JSON : ${snapshot.data.userId}');
} else
return CircularProgressIndicator();
}));
}
}
//这是后期课程
import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'package:http/http.dart' as http;
// To parse this JSON data, do
//
// final post = postFromJson(jsonString);
Post postFromJson(String str) => Post.fromJson(json.decode(str));
String postToJson(Post data) => json.encode(data.toJson());
// Nạp và phân tích cú pháp dữ liệu JSON
Future<Post> getPost(String url, String firebase_token) async {
Map<String, String> headers = {"Content-type": "application/json"};
String json =
'{"grant_type":"firebase_token","firebase_token":"$firebase_token"}';
final response = await http.post(url, headers: headers, body: json);
print('string 1');
print(response.body);
return postFromJson(response.body);
}
Future<http.Response> createPost(Post post, String url) async {
final response = await http.post('$url',
headers: {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.authorizationHeader: ''
},
body: postToJson(post));
return response;
}
class Post {
Post({
this.userId,
this.accessToken,
this.tokenType,
this.expiresUtc,
this.issuedUtc,
this.refreshToken,
this.roles,
});
String userId;
String accessToken;
String tokenType;
DateTime expiresUtc;
DateTime issuedUtc;
String refreshToken;
List<String> roles;
factory Post.fromJson(Map<String, dynamic> json) => Post(
userId: json["user_id"],
accessToken: json["access_token"],
tokenType: json["token_type"],
expiresUtc: DateTime.parse(json["expires_utc"]),
issuedUtc: DateTime.parse(json["issued_utc"]),
refreshToken: json["refresh_token"],
roles: List<String>.from(json["roles"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"user_id": userId,
"access_token": accessToken,
"token_type": tokenType,
"expires_utc": expiresUtc.toIso8601String(),
"issued_utc": issuedUtc.toIso8601String(),
"refresh_token": refreshToken,
"roles": List<dynamic>.from(roles.map((x) => x)),
};
}
只需进行相应更改即可。
让我知道它是否有效。