https://jsonplaceholder.typicode.com/posts
"\n\n\n <!--\n If you are serving your web app in a path other than the root, change the\n href value below to reflect the base path you are serving from.\n\n The path provided below has to start and end with a slash "/" in order for\n it to work correctly.\n\n For more details:\n * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base\n\n This is a placeholder for base href that will be replaced by the value of\n the --base-href
argument provided to flutter build
.\n -->\n <base href="/">\n\n <meta charset="UTF-8">\n <meta content="IE=Edge" http-equiv="X-UA-Compatible">\n <meta name="description" content="A new Flutter project.">\n\n \n <meta name="apple-mobile-web-app-capable" content="yes">\n <meta name="apple-mobile-web-app-status-bar-style" content="black">\n <meta name="apple-mobile-web-app-title" content="flutter_application_1">\n <link rel="apple-touch-icon" href="icons/Icon-192.png">\n\n \n <link rel="icon" type="image/png" href="favicon.png"/>\n\n flutter_application_1\n <link rel="manifest" href="manifest.json">\n\n \n // The value below is injected by flutter build, do not touch.\n var serviceWorkerVersion = null;\n \n \n <script src="flutter.js" defer>\n\n\n \n window.addEventListener('load', function(ev) {\n // Download main.dart.js\n _flutter.loader.loadEntrypoint({\n serviceWorker: {\n serviceWorkerVersion: serviceWorkerVersion,\n }\n }).then(function(engineInitializer) {\n return engineInitializer.initializeEngine();\n }).then(function(appRunner) {\n return appRunner.runApp();\n });\n });\n \n\n\n"
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
class JasonParsingSimple extends StatefulWidget {
const JasonParsingSimple({super.key});
@override
State<JasonParsingSimple> createState() => _JasonParsingSimpleState();
}
class _JasonParsingSimpleState extends State<JasonParsingSimple> {
@override
void initState() {
Future data;
// TODO: implement initState
super.initState();
data = getData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Parsing Jason"),
),
// body: Center(
// child: Container(
// child: FutureBuilder(
// future: getData(),
// builder: (context, AsyncSnapshot<dynamic> snapshot) {
// return Text(snapshot.data[0]['title']);
// }),
// )),
);
}
Future getData() async {
var data;
String url = "https://jsonplaceholder.typicode.com/posts";
Network network = Network(url);
data = network.fetchData();
// data.then((value) {
// print(value[3]);
// });
return data;
}
}
class Network {
final String url;
Network(this.url);
Future fetchData() async {
print("$url");
Response response = await get(Uri.parse("url"));
if (response.statusCode == 200) {
print(json.encode(response.body));
return json.encode(response.body);
} else {
print(response.statusCode);
}
}
}
instead of this,
Response response = await get(Uri.parse("url"));
write like this,
Response response = await get(Uri.parse(url));
cause: url
is a property of Network
class, this property actually holds the url address you assign from getData()
function. But "url"
is nothing but a string holds the value "url"
.