https://stackoverflow.com/questions/60487025/flutter-streambuilder-refresh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
// learn from https://www.digitalocean.com/community/tutorials/flutter-futures-and-streams-dart import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; //This allows us to convert the returned JSON data into something Dart can use. void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', home: MyHomePage(), ); } } class MyHomePage extends StatelessWidget { Future GetCountry(country) async { //String countryUrl = 'https://restcountries.eu/rest/v2/name/$country'; String countryUrl = 'http://176.122.178.74:3000/foods'; // http // .get(countryUrl) // // .then((response) => jsonDecode(response.body)[0]['name']) // .then((response) => jsonDecode(response.body)) // .then((decoded) => print(decoded)) // .catchError((error) => throw(error)); final response = await http.get('http://176.122.178.74:3000/foods'); // final jsonResponse = json.decode(response.body); Map<String, dynamic> map = json.decode(response.body); Map<String, dynamic> data = map["foods"]; for (final name in data.keys) { final value = data[name]; print('$name,$value'); // prints entries like "AED,3.672940" } // Map<String, dynamic> data1 = data["icecream"]; // print(data1["name"]); //List<dynamic> data1 = data["icecream"]; // final Map<String, dynamic> parsedmap = jsonDecode(jsonResponse); // for(var u in jsonResponse){ // print(u); // } } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: MaterialButton( onPressed: () => GetCountry('japan'), child: Container( color: Colors.blue, padding: EdgeInsets.all(15), child: Text('Get Country', style: TextStyle(color: Colors.white))), ), ), ); } } |