how can return future list in list of string?

Issue

i try to scrape web site to grab some data (Titles) then store it inside a list of string to use later in my text widget,but the problem is there is no data inside my list why that?
and when i use await to my dd list is show me error that i need to make my func future
i try to explaint more inside my code see it please
this is my code

// ignore: prefer_const_constructors

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:html/parser.dart';
import 'package:http/http.dart' as http;
import 'package:weatherapp/Test/datapars.dart';

class DataP extends StatelessWidget {
  late List<String> dd = [];

  Future<List<String>> arageek() async {
    List<String> title = [];
    var uri = Uri.parse('exmple.com');
    var respons = await http.get(uri);
    var doc = parse(respons.body);
    final className =
        doc.getElementsByClassName('WidePostCard_postTitle__SRltQ');

    for (final data in className) {
      title.add(data.text.toString());
      print(data.text); // it show me all the title in my console
    }
    return title; // when i print this its show me list of my all title i get
  }

  @override
  Widget build(BuildContext context) {
    dd = await arageek(); // here result is emty and tell cant use await
    print(dd);// show emty []
    return Container(
      alignment: Alignment.center,
      child: Text(
        'ddddddddddddd',
        style: TextStyle(fontSize: 20),
      ),
    );
  }
}

Solution

using FutureBuilder would solve your issues, this starter code might help you:

class DataP extends StatelessWidget {
  late List<String> dd = [];

  Future<List<String>> arageek() async {
    List<String> title = [];
    var uri = Uri.parse('exmple.com');
    var respons = await http.get(uri);
    var doc = parse(respons.body);
    final className =
        doc.getElementsByClassName('WidePostCard_postTitle__SRltQ');

    for (final data in className) {
      title.add(data.text.toString());
      print(data.text); // it show me all the title in my console
    }
    return title; // when i print this its show me list of my all title i get
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        // Initialize FlutterFire:
        future: arageek(),
        builder: (context, yourListOfStringResult) {
          // Check for errors
          if (yourListOfStringResult.hasError) {
            return Text("something is wrong!");
            
          } else   if (yourListOfStringResult.connectionState == ConnectionState.done) {
            List<String> data = yourListOfStringResult.data as List<String>; //
            return Container(
              alignment: Alignment.center,
              child: Text(
                'ddddddddddddd',
                style: TextStyle(fontSize: 20),
              ),
            );
          }else return CircularProgressIndicator();
        });
  }
}

Answered By – tareq albeesh

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.