Flutter Web Scraping RangeError(index):无效值:有效值范围为空:0

qyswt5oh  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(162)

我正在通过从网站上提取数据来制作测验应用程序。我无法解决在此过程中收到的范围误差错误。朋友在'items'中有50个项目。我想forEach不明白它是否完成了。我该怎么做?所以它给出了这个错误。我试着用它,但我不知道怎么做。RRangeError(index):无效值:有效值范围为空:0请帮帮我

import 'dart:async';
    import 'dart:convert';
    import 'package:get/get.dart';
    import 'package:flutter/material.dart';
    import 'package:http/http.dart' as http;
    import 'package:html/parser.dart' as parser;
    
    import '../../../models/sorular.dart';
    import 'bitisekran/bitis_ekrani.dart';
    
    void main() => runApp(sorubir());
    
    class sorubir extends StatefulWidget {
      const sorubir({super.key});
      @override
      State<sorubir> createState() => _sorubirState();
    }
    
    class _sorubirState extends State<sorubir> {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            appBar: AppBar(
              leading: BackButton(
                onPressed: () => Navigator.of(context).pop(),
              ),
              title: Text('Güncel Çıkmış Sorular'),
            ),
            body: sorubirIskele(),
          ),
        );
      }
    }
    
    class sorubirIskele extends StatefulWidget {
      @override
      State<sorubirIskele> createState() => _sorubirIskeleState();
    }
    
    _sorubirIskeleState benimTestim = _sorubirIskeleState();
    class _sorubirIskeleState extends State<sorubirIskele> {
      List<Sorular> sorular = [];
      var soruIndex = 0;
      bool isLoading = false;
      var url = Uri.parse('https://www.ehliyettestleri.com/coz/24ha2023e');
    
      Future getData() async {
        setState(() {
          isLoading = true;
        });
        var res = await http.get(url);
        final String body = utf8.decode(res.bodyBytes);
        final document = parser.parse(body);
        var response = document
            .getElementsByClassName('items')[0]
            .getElementsByClassName('mtq_question mtq_scroll_item-1')
            .forEach((element) {
          setState(() {
            sorular.add(Sorular(
              soruno: element.children[0].text.toString(),
              sorugetir: element.children[1].text.toString(),
              imggetir:
                  element.querySelector('p > img')?.attributes['src'].toString(),
              agetir: element.children[2].children[1].children[0].children[1].text
                  .toString(),
              bgetir: element.children[2].children[1].children[1].children[1].text
                  .toString(),
              cgetir: element.children[2].children[1].children[2].children[1].text
                  .toString(),
              dgetir: element.children[2].children[1].children[3].children[1].text
                  .toString(),
            ));
          });
          setState(() {
            isLoading = false;
          });
        });
      }
gmxoilav

gmxoilav1#

首先,你最好提供更多关于你的扑动医生和pubspec细节的信息。其次,这里的问题是document.getElementsByClassName('items')[0],它无法在html DOM中找到任何名为items的类。此外,url应该像这样解析final url = Uri.tryParse('www.ehliyettestleri.com/coz/24ha2023e');

相关问题