-
-
Save mmanflori/1e112cc486f868a772c8da3f96fbead6 to your computer and use it in GitHub Desktop.
komplettes async Beispielt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:convert'; | |
import 'package:http/http.dart' as http; | |
void main() => runApp(AsyncBeispiel()); | |
class AsyncBeispiel extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: HauptSeite(), | |
); | |
} | |
} | |
class HauptSeite extends StatefulWidget { | |
@override | |
_HauptSeiteState createState() => _HauptSeiteState(); | |
} | |
class _HauptSeiteState extends State<HauptSeite> { | |
String zeichenKette = ""; | |
@override | |
initState(){ | |
textVomServer(); | |
super.initState(); | |
} | |
textVomServer() async{ | |
String url = "https://rickandmortyapi.com/api/character/?name=pencil"; | |
http.Client client = http.Client(); | |
http.Response response = await client.get(url,); | |
Map decodedJson = json.decode(response.body); | |
setState((){ | |
zeichenKette = decodedJson["results"][0]["name"]; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Async Beispiel"), | |
), | |
body: Container( | |
child: Center( | |
child: Text( | |
zeichenKette | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment