如何显示变量user的值,并将其显示在文本小部件中,下面是源代码。该值已在终端中打印
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
class Home extends StatefulWidget {
const Home({Key key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
//get valueID => null;
@override
void initState() {
super.initState();
getStringValuesSF();
}
getStringValuesSF() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var stringValue = prefs.getString('currentUserId');
String apiurl = "http://192.168.161.144/api/dash.php";
var response = await http.post(Uri.parse(apiurl), body: {
"userid": stringValue,
});
if (response.statusCode == 200) {
var data = json.decode(response.body);
if (data["success"] == true) {
String user;
String userpics;
user = data["uname"];
userpics = data["upic"];
print(user);
print(userpics);
} else if (data["success"] == '10') {
print('not connected from DB');
}
} else {
print('not connected');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("HomePage")),
body: Container(
child: Text("Welcome User", style: TextStyle(color: Colors.black)),
),
);
}
}
2条答案
按热度按时间h5qlskok1#
你需要定义一个新的变量,并在你的API响应准备好时更新它,就像这样:
kyks70gy2#
可以创建模型类您,
并将FutureBuilder用于未来方法
有关使用FutureBuilder的详细信息