Last active
April 30, 2021 12:08
-
-
Save Maverick-I0/0c3cfd8cb09fb4ab706b1c994e4ba9b3 to your computer and use it in GitHub Desktop.
The App gets the result HTTP request and then writes it to a local JSON file.
This file contains hidden or 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 'dart:convert'; | |
| import 'dart:io'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:http/http.dart'; | |
| import 'package:path_provider/path_provider.dart'; | |
| void main() { | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| // This widget is the root of your application. | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Json http request', | |
| theme: ThemeData(), | |
| home: MyHomePage(), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| @override | |
| _MyHomePageState createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> { | |
| @override | |
| void initState() { | |
| super.initState(); | |
| } | |
| int listLength; | |
| List content = []; | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: FutureBuilder( | |
| future: readFile(), | |
| builder: (BuildContext context, process) { | |
| Widget builder; | |
| if (process.hasData) { | |
| builder = Container( | |
| width: MediaQuery.of(context).size.width, | |
| height: MediaQuery.of(context).size.height, | |
| color: Colors.amber, | |
| child: ListView.builder( | |
| itemCount: listLength, | |
| itemBuilder: (context, index) => Padding( | |
| padding: const EdgeInsets.all(8.0), | |
| child: ListTile( | |
| tileColor: Colors.pink, | |
| title: Text(getTitle(index)), | |
| //subtitle: Text(getDescription(index)), | |
| leading: Text( | |
| getId(index), | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| } else { | |
| builder = Container( | |
| width: MediaQuery.of(context).size.width, | |
| height: MediaQuery.of(context).size.height, | |
| color: Colors.amber, | |
| child: Center( | |
| child: Container( | |
| width: 100.0, | |
| height: 100.0, | |
| child: CircularProgressIndicator())), | |
| ); | |
| } | |
| return builder; | |
| }, | |
| ), | |
| ); | |
| } | |
| readFile() async { | |
| // Downloading the file before reading; | |
| await downloadFile(); | |
| // read the file: | |
| String path = await getPath(); | |
| String jsonFile = File('$path/jsonFile.json').readAsStringSync(); | |
| Map dJson = json.decode(jsonFile); | |
| List editorial = dJson['editorial']; | |
| // Storing the read data in local variables. | |
| listLength = editorial.length; | |
| return content = editorial; | |
| } | |
| getId(index) { | |
| Map data = content[index]; | |
| return data["Id"]; | |
| } | |
| getTitle(index) { | |
| Map data = content[index]; | |
| return data["title"]; | |
| } | |
| getDescription(index) { | |
| Map data = content[index]; | |
| return data["description"]; | |
| } | |
| } | |
| downloadFile() async { | |
| // Your stored url. | |
| String url = "add url"; | |
| // Getting the http response | |
| Response response = await get(url); | |
| int statusCode = response.statusCode; | |
| print(statusCode); // for debugging purpose | |
| //This will remove the html tags if any | |
| String body = response.body.replaceAll( | |
| RegExp( | |
| r"<[^>]*>", | |
| multiLine: true, | |
| caseSensitive: true, | |
| ), | |
| ''); | |
| //Writing the body to a json | |
| String path = await getPath(); | |
| File jsonFile = File('$path/jsonFile.json'); | |
| jsonFile.writeAsStringSync(body); | |
| } | |
| Future<String> get applicationSupportFileDirectoryPath async { | |
| String applicationSupportDirectoryPath; | |
| final getApplicationDirectory = await getApplicationSupportDirectory(); | |
| applicationSupportDirectoryPath = getApplicationDirectory.path; | |
| return applicationSupportDirectoryPath; | |
| } | |
| getPath() async { | |
| String path; | |
| await applicationSupportFileDirectoryPath.then((value) { | |
| path = value; | |
| }); | |
| print('[P] path returned : $path'); | |
| return path; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment