How can i display the value from the variable user and display it in the text widget, here the source code below. The value already printed in the terminal
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
Answers
You need to define new variable and update it when ever your api response get ready, like this:
You can create a model class,
And use FutureBuilder for future method
More about using FutureBuilder