Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Web Scraping in Flutter | how to fetch specific image from a website in flutter

Want To Watch Video?

Step 1:

Create a new dart file and code in that file.

import 'package:flutter/material.dart';
import 'package:flutter_share/flutter_share.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:http/http.dart' as http;
import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart' as parser;
import 'package:url_launcher/url_launcher.dart';

class UpdatePost extends StatefulWidget {
  @override
  _UpdatePostState createState() => _UpdatePostState();
}

class _UpdatePostState extends State {
  List title = List();
  List post = List();
  List link = List();

  List image = List();

  void _getDataFromWeb() async {
    final response = await http.get('https://arprogramming.blogspot.com/');
    dom.Document document = parser.parse(response.body);
    final link2 = document.getElementsByClassName('entry-title');
    final content = document.getElementsByClassName('entry-content');
    final elements = document.getElementsByClassName('entry-header blog-entry-header');

    final ImageElement = document.getElementsByClassName('entry-image-link');
    setState(() {
      title = elements
          .map((element) =>
      element.getElementsByTagName("a")[0].innerHtml)
          .toList();
      post = content
          .map((element) =>
      element.getElementsByTagName("p")[0].innerHtml)
          .toList();
      link = link2
          .map((element) =>
      element.getElementsByTagName("a")[0].attributes['href'])
          .toList();

      image = ImageElement
          .map((element) =>
      element.getElementsByTagName("span")[0].attributes['data-image'])
          .toList();
    });
  }

  Future share(dynamic link,String title) async {
    await FlutterShare.share(
        title: 'Codding Application',
        text: title,
        linkUrl: link,
        chooserTitle: 'Where You Want to Share'
    );
  }

  @override

  void initState() {
    _getDataFromWeb();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black87,
      body: post.length==0? Text("No data",
          style: TextStyle(color: Colors.white)) : ListView.builder(
        itemCount: post.length,
        itemBuilder: (context, index){
          return AnimationConfiguration.staggeredList(
            position: index,
            duration: const Duration(milliseconds: 375),
            child: SlideAnimation(
                child: FadeInAnimation(
                    child: GestureDetector(
                      onTap: () async {
                        dynamic url=link[index];
                        if (await canLaunch(url))
                          launch(url);
                        else
                        {
                          print('error');
                        }
                      },
                      child: Padding(
                        padding: const EdgeInsets.all(10),
                        child: Card(
                          child: Container(
                            color: Colors.black87,
                            child: Column(
                              children: [
                                Container(
                                  child: Image.network(
                                    image[index],
                                    scale: 0.1,
                                  ),
                                ),
                                Align(
                                  alignment: Alignment.centerLeft,
                                  child: Text(title[index],
                                    style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      color: Colors.red,
                                      fontSize: 20,
                                    ),
                                  ),
                                ),
                                SizedBox(height: 15),
                                Text(
                                  post[index],
                                  style: TextStyle(
                                    color: Colors.white,
                                  ),
                                ),
                                Row(
                                  children: [
                                    FlatButton(
                                      shape: RoundedRectangleBorder(
                                          borderRadius: BorderRadius.circular(5.0),
                                          side: BorderSide(color: Colors.white)
                                      ),
                                      onPressed: (){
                                        share(link[index],title[index]);
                                      },
                                      child: Text("Share With Friends",
                                        style: TextStyle(
                                          color: Colors.red,
                                        ),
                                      ),
                                    ),
                                    SizedBox(width: 10),
                                    FlatButton(
                                      shape: RoundedRectangleBorder(
                                        borderRadius: BorderRadius.circular(5.0),
                                          side: BorderSide(color: Colors.white)
                                      ),
                                      onPressed: ()async{
                                        dynamic url=link[index];
                                        if (await canLaunch(url))
                                        launch(url);
                                        else
                                        {
                                        print('error');
                                        }
                                      },
                                      child: Text("Explore Post",
                                        style: TextStyle(
                                          color: Colors.red,
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    )
                )
            ),
          );
        },
      ),
    );
  }
}

Step 2:

Call this class from main.dart:

import 'package:codding_app/coddingApp/SplashScreen1.dart';
import 'package:flutter/material.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: 'Flutter Demo',
      home: UpdatePost (),
    );
  }
}

OUTPUT:





This post first appeared on AR Programming, please read the originial post: here

Share the post

Web Scraping in Flutter | how to fetch specific image from a website in flutter

×

Subscribe to Ar Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×