6

We know there is a Jsoup library for android developers to parse html text, code etc. As I am new in flutter mobile app development I want to know if there is any library like Jsoup to parse html text,code from a web site in flutter.

  • It depends on your purpose. if you just want to display a webpage, use this plugin https://pub.dartlang.org/packages/flutter_webview_plugin . what is your use case which this plugin cannot do? – Yamin Aug 11 '18 at 10:26
  • No, actually I want to display only a few text or content like a headline of a news from a newspaper website, not the full webpage. – N. I. Md. Ashafuddula Aug 11 '18 at 12:02
  • Still, use that plugin, use md. – Yamin Aug 11 '18 at 12:24

2 Answers2

10

You can parse a HTML string this way

import ‘package:html/parser.dart’;
//here goes the function 

String _parseHtmlString(String htmlString) {

var document = parse(htmlString);

String parsedString = parse(document.body.text).documentElement.text;

return parsedString;
}

Please let me know this doesn’t solve your problem.

Jaswant Singh
  • 9,900
  • 8
  • 29
  • 50
2

I solve the problem for me. Firstly, to user parser you should fetch data using http.get(url). After that you can parse what you want.

Fetch html page:

Future<String> fetchHTML(String url) async {
  final response = await http.get(url);

  if (response.statusCode == 200)
    return response.body;
  else throw Exception('Failed');
}

After that you must call FutureBuilder()

    FutureBuilder<String>(
      future: fetchHTML('http://your_page.ru/page.html'),
      builder: (context, snapshot){
        if (snapshot.hasData) {
          //Your downloaded page
          _temp = snapshot.data;
          print(snapshot.data);
          return Text('Finished');
        }
        else if (snapshot.hasError)
          return Text('ERROR');

        return Text('LOADING');
      },
    ),

And now you can parse it:

parse(_temp);
Alex Adrianov
  • 312
  • 1
  • 8
  • hey i'm in your exact situation but the get functions throw me an error that i think is related to the request header but don't know how to fix. any idea? – Bonfra Apr 22 '20 at 22:18