#flutter #dart
#flutter #dart
Вопрос:
Я пытаюсь объединить два списка, но получаю сообщение об ошибке, я много искал в Интернете, но не могу найти решение своей проблемы. Пожалуйста, помогите мне. Спасибо.
это ошибка в цикле for
for (var i = 0; i < titleList.length; i ) {
titleList[i]['src'] = thumbnail[i]['1'];
}
Unhandled exception:
type 'String' is not a subtype of type 'int' of 'index'
#0 main
bindart_webscrape2.dart:34
<asynchronous suspension>
#1 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#2 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
Что я пробовал, так это
import 'package:web_scraper/web_scraper.dart';
import 'dart:convert';
void main() async {
final webScraper = WebScraper('https://w22.holymanga.net');
// Response of getElement is always List<Map<String, dynamic>>
List<Map<String, dynamic>> titleLink;
List<Map<String, dynamic>> thumbnails;
// Loads web page and downloads into local state of library
if (await webScraper.loadWebPage('/genre/full-color/page-2/')) {
// getElement takes the address of html tag/element and attributes you want to scrape from website
// it will return the attributes in the same order passed
titleLink = webScraper
.getElement('div.comics-grid > div.entry > a.thumb', ['title', 'href']);
thumbnails = webScraper
.getElement('div.comics-grid > div.entry > a.thumb > img', ['src']);
}
var titleList = [];
var thumbnail = [];
titleLink.forEach((element) {
final title = element['attributes']['href'];
titleList.add('$title');
});
thumbnails.forEach((element) {
final thumb = element['attributes'];
thumbnail.add('$thumb');
});
for (var i = 0; i < titleList.length; i ) {
titleList[i]['src'] = thumbnail[i]['1'];
}
}
Пожалуйста, помогите мне.
Спасибо
Комментарии:
1. ваш
thumbnail
иtitleList
оба являются массивом строк.titleList[i]['src'] = thumbnail[i]['1']
это невозможно сделать для строковых значений.
Ответ №1:
titleLink.forEach((element) {
final title = element['attributes']['href'];
titleList.add('$title'); // You're adding a string to the list
});
for (var i = 0; i < titleList.length; i ) {
titleList[i]['src'] = thumbnail[i]['1']; // But you're trying to access it as if it was a map
}
Если ['attributes']['href']
это map, то вместо titleList.add('$title')
do titleList.add(title)