Flutter Web Scraper Level 2: Etsy Product Finder Part 1
Tags: flutter, android, ios, webscraping
Published: 2020-07-01 11:59:18


## Intro

In my previous post ([post](https://dev.to/dailyfluttermonster/get-data-from-the-web-with-web-scraper-and-flutter-gki)) I showed how we can use web scraper to get the current world population number and display, however, this is a pretty simple and useless idea. So no I want to show how to use web scraper to make an app that is more like what you would be used to seeing. The application is called Etsy Product Finder and it will allow users to search for a product and view the top 5 Etsy results with their prices and if the user wants to view the product on the Etsy website, a clickthrough will be implemented. Since this will be a long walk through I will be posting 2 parts, the first one will cover installing all the packages and setting up the ui. The second will focus on how to use the data by web scraping the etsy website.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cz5gs30v4hmd6jeivtqt.png)

## Tutorial Portion

#### Installing Packages

The first thing we need to do is add the packages that we need to our pubspec yaml. In this case it will be [web_scraper](https://pub.dev/packages/web_scraper) and [url_launcher](https://pub.dev/packages/url_launcher), these packages allow us to do the necessary scraping data and also then allowing the user to clickthrough to open the item's page in the browser


{%gist https://gist.github.com/Mattds825/ba7c9a6cab82a6b77278a8f0db2bd354 %}

Then in main.dart you do the usual and clear everything out leaving only the main function and a stateless widget that has it's home being you homescreen:

{%gist https://gist.github.com/Mattds825/eec2435494971e91454e7608e86820e5 %}

Now I will first breakdown what we are going to build in the first part:

- Main Screen with a search bar at the top
- List to contain search result
- List items for the result list

In this part we will be going through only the UI elements of the app

#### Creating Homescreen

We need to create a homescreen with a search bar at the top and a list underneath it.

So we are first going to have

{%gist https://gist.github.com/Mattds825/59334165fb398cf354dd084c5c51e9bf %}


This create a homescreen with a scaffold within a safe area (widget that avoid notches and notification bar), and it has a column, whose only child is the appbar currently.

_HomeScreen state contains the variables for if the search was has been made and the string for the search

As you will notice in the Homescreen class we create _HomeScreenState as a variable (state) then pass it on to createState(), this is because we are going to want to be able to change the state from the search bar class. which is what we will be doing next.

#### Creating the Seachbar

For the search bar, we will use a row that contains a texform field and a material button that will trigger the search.

{%gist https://gist.github.com/Mattds825/bd82608beebda0c720135880610ae8c5 %}

In the code above you can see that we pass an instance of homescreen to the search bar, in turn when the material button is pressed, we change the homescreen's state so that searched is true and, the search text is the text from the textformfeild.

Next we will design the result item and the result list.

#### Result Item

The result item has to include variables for:

- String for item's image url
- String for item's description
- String for item's clickthrough url
- String for item's prices

{%gist https://gist.github.com/Mattds825/090120338d7a4f5a0f49bc119e100cf3 %}

For the text we use a null checker to make sure we are not passing null results. The whole thing is wraped in an GestureDetector that calls _lauchUrl(). This in turn ia an async method that calls launch(url) to open the clickthough link.

Now to create a list of these 

#### Result List

For testing puproses we won't webscrape yet, instead for this part we generate 5 test list items just to see what it looks like.

{%gist https://gist.github.com/Mattds825/2db7934278b1df200950ec699dcc7d2b  %}

Then in test_screen.dart we can add to the column:

{%gist https://gist.github.com/Mattds825/a40d030c6c88077b7b50eb43a6748e89 %}

if we run this and click search we will see:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mww4wvo7asey6ly7960r.png)

## Conclusion

In the next part I will show how we can add the webscraping to scrape search results from etsy

[source code](https://github.com/Mattds825/esty_product_searcher)