'returning null value when flutter http get request(Instances of _<Future> dynamic)
i am trying to get a data from a flask rest api but it keeps returning null value when it converted toString() it displays "Instances of _ dynamic"
getPass() async {
var response = await http.get(Uri.http("127.0.0.1:5000","generate"));
var jsonData = jsonDecode(response.body);
var pass = jsonData['Password'];
return pass;
}
onPressed: () {
getPassword = getPass().toString();
setState(() {
finalPassword = "hi $getPassword";
});
},
.....
Center(child: Text(
finalPassword,
//textAlign: TextAlign.center,
style: const TextStyle(
fontFamily: 'OpenSansCondensedBold',
fontSize: 23,
color: Colors.red,
),
)),
api code
@app.route('/generate', methods=['GET', 'POST'])
def getPassword(): # put application's code here global response d={}
if request.method == "POST":
input1 = request.form['Input1']
input2 = request.form['Input2']
input3 = request.form['Input3']
response = Generation.GeneratePassword(input1, input2, input3)
return " "
else:
d['Password'] = response
return jsonify(d)
Solution 1:[1]
you should await your call to getPass as it returns a future:
onPressed: () async {
getPassword = await getPass();
getPasswordString = getPassword.toString();
setState(() {
finalPassword = "hi $getPasswordString";
});
},
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |