Extract Future<Object> variables in Flutter?

Viewed 18

I am currently creating a profile system, in which users can edit certain attributes. I can already GET any and all information that has previously been entered by the user, as well as updating each observation in the database. I am attempting to print the given values to the screen, so the user can see their previously saved input before choosing to change.

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  final _formKey = GlobalKey<FormState>();
  StorageService storage = StorageService();
  userInfo info = userInfo(0, 0,"","", "", "", "");

  Future<userInfo> fetchUserInfo() async {
    String jwt = await storage.readSecureData("jwt") as String;
    Map<String, dynamic> map = Jwt.parseJwt(jwt);
    String url = 'http://localhost:8087/getinfo/' + map["id"];
    final response = await http.get(Uri.parse(url));
    if (response.statusCode == 200) {
      // If the server did return a 200 OK response,
      // then parse the JSON.
      print(response.body);
      // info = userInfo.fromJson(jsonDecode(response.body));
      return userInfo.fromJson(jsonDecode(response.body));
    } else {
      // If the server did not return a 200 OK response,
      // then throw an exception.
      throw Exception('Failed to load userInfo');
    }
  }

This is my GET request. It works perfectly and returns an userInfo object.

@override
  Widget build(BuildContext context) {
    userInfo info = userInfo(0, 0,"","", "", "", "");
    final ButtonStyle style =
    ElevatedButton.styleFrom(
      primary: Colors.green,
        fixedSize: Size(200, 50),
      textStyle: const TextStyle(fontSize: 20),
    );


    return Padding(
        padding: EdgeInsets.all(0),
        child: ListView(
          children: <Widget>[
          Container(
            height: 410,
            margin: const EdgeInsets.fromLTRB(20, 20, 20, 20),
            padding: const EdgeInsets.fromLTRB(20, 20, 20, 20),
            decoration: BoxDecoration(
                color: Colors.blue,
                borderRadius: BorderRadius.all(Radius.circular(18))
            ),
            child: Column(
              children: <Widget> [
                const Text("Patient Details" , style: TextStyle(fontSize: 40, color: Colors.white)),
                Row(
                  children: [
                    const Text("Medicare:" , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
                    const Text("1234567891" , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
                  ]),
                Row(
                    children: [
                      const Text("Home Address:" , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
                      ]),
                Row(
                    children: [
                       Text(info.address , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
                    ]),
                Row(
                    children: [
                      const Text("Mobile:" , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
                      const Text("0447171002" , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
                    ]),
                Container(
                    margin: const EdgeInsets.fromLTRB(0, 25, 0, 0),
                  child: ElevatedButton.icon(
                    style: style,
                    icon: const Icon(Icons.edit),
                      label: const Text('Edit Personal'),
                    onPressed: () async {
                        Navigator.push(
                            context, MaterialPageRoute(builder: (context) => const EditInfoApp()));
                    },
                  )
            )]),
          ),
            Container(
              height: 470,
              margin: const EdgeInsets.fromLTRB(20, 20, 20, 20),
              padding: const EdgeInsets.fromLTRB(20, 20, 20, 20),
              decoration: BoxDecoration(
                  color: Colors.blue,
                  borderRadius: BorderRadius.all(Radius.circular(18))
              ),
              child: Column(
                  children: <Widget> [
                    const Text("Health Information" , style: TextStyle(fontSize: 40, color: Colors.white)),
                    Row(
                        children: [
                          const Text("Health Status: " , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
                          const Text("average" , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
                        ]),
                    Row(
                        children: [
                          const Text("Allergies:" , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
                        ]),
                    Row(
                        children: [
                          const Text("nuts, Peanuts, fish" , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
                        ]),
                    Row(
                        children: [
                          const Text("Diagnosis:" , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
                        ]),
                    Row(
                        children: [
                          const Text("Asthma, constipation" , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
                        ]),
                    Container(
                        margin: const EdgeInsets.fromLTRB(0, 25, 0, 0),
                        child: ElevatedButton.icon(
                          style: style,
                          icon: const Icon(Icons.edit),
                          label: const Text('Edit Health'),
                          onPressed: () {
                            Navigator.push(
                                context, MaterialPageRoute(builder: (context) => const EditHealthApp()));
                          },
                        )
                    )]),
            ),

        ]));
  }
}

At the moment, I have most fields hard coded. Although, in the home address field, i have tried to enter info.address. This obviously will not work, as I cannot initialise the userInfo object without being inside of an async function, but then I can't access the variables.

0 Answers
Related