I am trying to use http-requests to get Json data of a devicemanagement webserver. the webserver works wonderfully and is viewable publicly. the Database, Webserver and the Webapp are all in my cloud and working perfectly.
The Problem: i cant use "Future" as a String. i also cannot await the Future.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title:'Json Text',
home: Scaffold(
appBar: AppBar(
title:Text('JsonText') // yup, this appears.
),
body: Center(
child: Text((getText().toString())) // Returns "Instance of 'Future<String>'" should be "New"
),
));
}
i cannot "await getText()" since it is the function that builds the Flutter App, it simply returns an error if i try.
i have tried quite alot: return await m // doesnt work, still returns a Future Object. tried changing the return type to String // doesnt work, async throws an error.
Future<Modell> getJson() async {
// simple request for a single modell
var simple_response = await http.get("http://35.207.119.142:443/api/commands/1010"); // my url here
print("Station 3");
// this should be checked after EVERY http.get
if (simple_response.statusCode != 200) {
print("something went wrong");
} else {
print("everything is correct"); // In my Case, this does work.
}
print("Station 4");
Modell m = Modell.fromJson(jsonDecode(simple_response.body));
print("Station 5");
return m;
}
Future<String> getText() async {
print("Station 2");
Modell m = await getJson();
print("Station 6");
print(m.modell); // Properly returns: "New". thats what it should be.
print("Endstation");
return m.modell; // This is what i need Help With.
}
TLDR: what i need is an Instance of "String" instead of "Future", and i cannot use "await" since the build function cant do that.
This code is in Flutter, and i am supposed to make an App for a Webserver reachable at: 35.207.119.142 (When i turn it on.)
in order to better use this code, im leaving the webserver on for about a day.
here is the entirety of my Code: Actual App: https://del.dog/tetengoqex.txt Provided API (bad): https://del.dog/liwecramyd.txt Actual Webserver API: https://del.dog/v/uwestol (this contains a link to github)
so once again: my question is: how do i get the content of: m.model (m is an Instance of a Modell):
class Modell {
int geraeteID;
String modell;
int baujahr;
String seriennummer;
bool verfuegbar;
String historie;
String datumAusleihe;
String ausgeliehenAn;
String bemerkung;
bool aktiv;
I Thank you for contemplating the solution of this problem, i wish you a good day.
- Cuboid404
Solution Code: https://del.dog/garfapovil.txt
Solution: Use the FutureBuilder Widget. while it wasnt that easy to use, it was better than using .this()
home: Scaffold(
appBar: AppBar(
title:Text('JsonText') // yup, this appears.
),
body: Center(
child: FutureBuilder(
future: getText(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data,style: Theme.of(context).textTheme.headline);
} else if (snapshot.hasError) {
return Text("${snapshot.error}",style: Theme.of(context).textTheme.headline);
} else {
return CircularProgressIndicator();
}})